这是我的模式,用户id,书签,bookmarkable_id,bookmarkable_type,user_id,文章,帖子,id,你可以看到,它是一个多态关系。我为它写了一个关系,但它返回[];(用户至少有一个书签帖子) User.php public function bookmarks() return $this->morphManyfunction bookmarks() return $this->hasOne(Bookmark::class,
当我使用带有Laravel Eloquent的ModelName::with('somerelation')->get()时,如果模型没有这种关系,我会得到Call to undefined relationship但是对于多态关系,我得到了所有相关模型的集合,如果没有定义关系,我想使用with('somerelation')和null。有没有办法避免错误并从with()返回null,或者有条件地使用呢?
我和Laravel的关系是多态的。我的多态关系是Message->messageable成为Group或Chat。从示例Group::first()->with(‘messages’)开始,->get()将返回一个空的消息数组。extends Modelpublic function messages(){这些