インストール
$ 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');
}