Aug 18, 2024
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('author_id')->constrained('users');
});
Post::factory()->count(10)->create();
User::factory()->count(5)->create();
Post::factory()->count(100)->create();
User
dan Post
:
public function posts() {
return $this->hasMany(Post::class);
}
public function author() {
return $this->belongsTo(User::class);
}
$post = Post::first();
$authorName = $post->author->name;
$user = User::first();
$posts = $user->posts;