前回の BBS は操作が分かりずらかったので作り直しました。デザインも変わりました。
今回は、BBS にデータ構造を作り、初期データを設定します。データベースとして SQLite を使います。 Laravel の初期設定では、データベースは SQLite になっています。
データ構造を作っていきます。database / migrations の実際の日付がついた _create_posts_table.php を開いて、up メソッドに、$table->string('body'); を書き足します。一般的に本文は body という名前を付けます。1行の文字列は string、複数行の文字列は text と指定します。
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->text('body'); // ここ
$table->timestamps();
});
}
続けてコメント用のデータ構造も作ります。database / migrations の実際の日付がついた _create_comment_table.php を開いて、up メソッドに、$table->string('title'); と $table->foreignId('post_id')... を書き足します。foreignId のほうに、post データとの関係IDが入ります。
public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->string('body'); // ここと
$table->timestamps();
$table->foreignId('post_id')->constrained()->cascadeOnDelete(); // ここ
});
}
constraind で、存在しない ID とは関係を作らないようにします。cascadeOnDelte で、元の投稿がなくなれば、コメントもすべてなくなるようにしています。
php artisan migrate
# 次のように表示されれば OK です
2025_07_27_000442_create_posts_table ................................................... 2.82ms DONE
2025_07_27_000505_create_comments_table ................................................ 0.74ms DONE
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
// ここから
protected $fillable = [
'body',
];
// ここまで
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
// ここから
protected $fillable = [
'body',
'post_id',
];
// ここまで
}
前述の作業ではデータ構造が作成されただけです。初期データを設定するには次のようにします。
php artisan tinker
> // プロンプトが変わります
# データを挿入します
App\Models\Post::create(['body' => '本文 1']);
# 次のように表示されれば OK です
= App\Models\Post {#6290
body: "本文 1",
updated_at: "2025-07-28 11:13:04",
created_at: "2025-07-28 11:13:04",
id: 1,
}
# 続けてデータを挿入します
App\Models\Post::create(['body' => '本文 2']);
App\Models\Post::create(['body' => '本文 3']);
App\Models\Comment::create(['body' => 'コメント 1', 'post_id' => 1]);
App\Models\Comment::create(['body' => 'コメント 2', 'post_id' => 1]);
App\Models\Comment::create(['body' => 'コメント 3', 'post_id' => 2]);
App\Models\Comment::create(['body' => 'コメント 4', 'post_id' => 3]);
# tinker を終了します
exit
php artisan migrate:status
Migration name ..................................................................... Batch / Status
0001_01_01_000000_create_users_table ...................................................... [1] Ran
0001_01_01_000001_create_cache_table ...................................................... [1] Ran
0001_01_01_000002_create_jobs_table ....................................................... [1] Ran
2025_07_28_105548_create_posts_table ...................................................... [2] Ran
2025_07_28_105601_create_comments_table ................................................... [2] Ran
Post に対しては posts、Comment に対しては comments というように自動的に複数形になります。しかし BBS などの、どのようにして複数形にすれば分からないものは、b_b_s
などの名前になります。一応 b_b_s_id などの名前でも初期データは挿入できます。しかしその後のコーディングで単数形と複数形を使い分ける場面で困ることになります。複数形にならないようなデータ名は避けたほうが良いでしょう。