LoginSignup
0
0

More than 5 years have passed since last update.

Laravel5を使う上での個人的メモ

Last updated at Posted at 2017-02-01

インストール

$ laravel new projectname

コントローラ

$ php artisan make:controller SiteController
$ php artisan make:controller SiteController --resource

コントローラ名については単数系で良い様子。

x SitesController (複数形)
o SiteController (単数形)

Laravel 5.3 コントローラ: リソースコントローラ

マイグレーション

作成

参考: データベース:マイグレーション

$ php artisan make:migration create_sites
projectname/database/migrations/2016_12_09_064707_create_sites.php
public function up()
{
  // テーブルを更新
  // php artisan make:migration add_hogehoge_column_kigyos_table --table=kigyos
  Schema::create('sites', function (Blueprint $table) {
   $table->increments('id');
   $table->string('name', 256)->default(''); // サイト名
   $table->string('url', 1024); // サイトURL
   $table->timestamps();

  });
}

public function down()
{
  Schema::dropIfExists('sites');
}

更新

スケルトンファイル作成

$ php artisan make:migration sites_add_email

マイグレーションファイル編集

app/database/migrations/2016_12_15_055527_sites_add_email.php
 public function up()
 {
   Schema::table('sites', function (Blueprint $table) {
     $table->string('email')->after('email');
     $table->index( ['name', 'url'] );
   });
 }

 public function down()
 {
   Schema::table('sites', function (Blueprint $table) {
     $table->dropColumn('email');
     // Laravelで作られるインデックスは テーブル名、カラム名、indexをアンダーバーで繋げた文字列
     // 全て小文字
     $table->dropIndex( 'sites_name_url_index' );
   });
 }

マイグレーション実行

$ php artisan migrate

シード

$ php artisan make:seeder SitesTableSeeder
projectname/database/seeds/SitesTableSeeder.php
public function run()
{
  DB::table('kigyos')->insert(
    [
      [
        'name' => 'test1',
        'url'  => 'http://example.com',
      ],
      [
        'name' => 'test2',
        'url'  => 'http://test2.example.com',
      ],
    ]
 );
}

テスト

$ php artisan make:test SiteTest
/tests/SiteTest.php
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->visit('/sites')
             ->see('index');
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0