Laravelで簡単なCMSを作ってみます。
環境構築は以下の記事を参考にどうぞ。
マイグレーションファイル作成
テーブル構成・定義はCakePHPのチュートリアルで紹介されているものを拝借し、3つテーブルを作成します。Laravelにはマイグレーションというデータベースの管理機能が提供されているので、これを使用します。
まずはartisan
コマンドを使用してマイグレーションファイルを生成します。
php artisan make:migration create_articles_table
php artisan make:migration create_tags_table
php artisan make:migration create_article_tag_table
テーブル定義作成
作成されたマイグレーションファイルに各テーブルの定義を加えていきます。
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->integer('user_id');
$table->string('title', 255);
$table->string('slug', 191);
$table->text('body');
$table->boolean('published')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('title', 191);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateArticleTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('article_tag', function (Blueprint $table) {
$table->integer('article_id');
$table->integer('tag_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('article_tag');
}
}
マイグレーションファイルが作成できたらmigrate
Artisanコマンドを実行してデータベースに反映させます。
php artisan migrate
これだけでテーブルを作成できます。
おまけ
テーブル作成後にテストデータを追加したい場合は、Laravelのシーディング
を利用します。
テストデータとしてusers
テーブルにuserを作成してみます。
シーダーファイルを作成
マイグレーションと同様にartisanコマンドを実行してシーダファイルを作成します。コマンド実行後database/seeders
にシーダファイルが生成されます。
php artisan make:seeder UserSeeder
シーダファイルが生成されたら、データ挿入処理を追加します。
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => 'john',
'email' => 'john@gmail.com',
'password' => Hash::make('MyPassword'),
]);
DB::table('users')->insert([
'name' => 'jack',
'email' => 'jack@gmail.com',
'password' => Hash::make('MyPassword'),
]);
}
}
シーダの呼び出し
artisanコマンドを使用して個別にシーダファイルを実行することができますが、DatabaseSeeder
クラスのcall
メソッドにシードクラスを登録することで、シーダファイルが複数になっても一括で実行できるようになります。
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// \App\Models\User::factory(10)->create();
$this->call([
UserSeeder::class,
]);
}
}
シーダの実行
db:seed
ArtisanコマンドでDatabase\Seeders\DatabaseSeeder
クラスを呼び出しシーディングを実行します。これによりテストデータが登録できます。
php artisan db:seed
テーブルの中身を確認したいとき、vscodeの拡張機能を使用すれば簡単にテーブルデータを確認することができます。