php artisan make:model -m Comment
public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->mediumText('content');
$table->timestamps();
// post_id est lié à la clé primaire de la table posts
$table->unsignedBigInteger("post_id");
$table->foreign("post_id")->references("id")->on("posts");
});
}
public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
// post_id est lié à la clé primaire de la table posts
$table->foreignId('post_id')->constrained();
});
}
Dans app/Models/Post.php
class Post extends Model
{
use HasFactory;
public function comments()
{
return $this->hasMany(Comment::class);
}
}
@foreach ($posts->comments as $comment)
<p>{{ $comment->content }}</p>
@endforeach