调用未定义的方法Illuminate \ Database \ Eloquent \ Builder :: morphWith

时间:2019-10-05 12:22:23

标签: laravel eloquent eager-loading laravel-5.8

遇到以下代码麻烦

$feed = Feed::orderBy('updated_at', 'desc')
                    ->with(['feedable' => function (MorphTo $morphTo) {
                        $morphTo->morphWith([
                            Post::class => ['user', 'images', 'likes', 'comments', 'comments.user'],
                            GroupPost::class => ['user', 'images', 'likes', 'comments', 'comments.user', 'group']
                        ]);
                    }])
                    ->paginate(10);

https://laravel.com/docs/5.8/eloquent-relationships#eager-loading

获得用户的参考

没有得到什么地方出了问题。 出现类似调用未定义方法Illuminate \ Database \ Eloquent \ Builder :: morphWith

的错误

虽然延迟加载可以正常工作

$feed = Feed::orderBy('updated_at', 'desc')
                    ->with('feedable')
                    ->paginate(10)
                    ->loadMorph('feedable', [
                        Post::class => ['user', 'images', 'likes', 'comments', 'comments.user'],
                        GroupPost::class => ['user', 'images', 'likes', 'comments', 'comments.user', 'group'],
                    ]);

请帮助解决此问题。

0 个答案:

没有答案