Complete the code to define a one-to-many relationship in a Laravel model.
public function posts() {
return $this->[1](Post::class);
}In Laravel, a one-to-many relationship is defined using the hasMany method.
Complete the code to define the inverse of a one-to-many relationship in a Laravel model.
public function user() {
return $this->[1](User::class);
}The inverse of a one-to-many relationship is defined with belongsTo in Laravel.
Fix the error in the code to correctly define a many-to-many relationship in a Laravel model.
public function roles() {
return $this->[1](Role::class);
}Many-to-many relationships in Laravel use the belongsToMany method.
Fill both blanks to define a one-to-one relationship and access the related model's attribute.
public function profile() {
return $this->[1](Profile::class);
}
$name = $user->profile->[2];A one-to-one relationship uses hasOne. Accessing the related model's name attribute shows the user's profile name.
Fill all three blanks to create a dictionary of post titles and their authors' emails for posts with more than 100 likes.
return collect($posts)->filter(fn($post) => $post->likes > [1]) ->mapWithKeys(fn($post) => [ $post->[2] => $post->user->[3] ]);
This code filters posts with more than 100 likes, then creates a dictionary with post titles as keys and authors' emails as values.