Complete the code to define a polymorphic relation method in a Laravel model.
public function image() {
return $this->[1]('imageable');
}In Laravel, the morphTo method defines the inverse of a polymorphic relation.
Complete the code to define a polymorphic relation in the Image model.
public function imageable() {
return $this->[1]();
}The morphTo method is used in the polymorphic child model to access the parent model.
Fix the error in the polymorphic relation definition in the Post model.
public function comments() {
return $this->[1]('commentable');
}Use morphMany in the parent model to define a polymorphic one-to-many relation.
Fill both blanks to complete the polymorphic relation in the Video model.
public function tags() {
return $this->[1]('taggable');
}
public function taggable() {
return $this->[2]();
}The morphToMany method defines a polymorphic many-to-many relation in the parent model, while morphTo accesses the parent in the related model.
Fill all three blanks to complete the polymorphic relation and query in Laravel.
$comments = Comment::where('[1]_id', $post->id) ->where('[2]_type', get_class($post)) ->get(); $post->[3]()->save(new Comment(['body' => 'Nice post!']));
Polymorphic relations use commentable_id and commentable_type columns to query related models. The relation method comments is used to save new comments.