Eager loading with route model binding

旧巷老猫 提交于 2019-12-06 03:32:51

问题


I have a controller function like this

public function show(NovelRequest $request, Novel $novel)
{
    // load the chapters
    $novel->chapters;

    // return the detail view of a novel
    return view('novels.show', compact('novel'));
}

I receive a novel object because i m using route model binding. However, I'd like to load more than the chapters. Since it would cause many request if i now do something like

$novel->chapters;
$novel->bookmarks;
...

I wondered if theres a way to load "multiple" relations when i already have the novel object. Usually i would to something like

Novel::with('chapters', 'bookmarks')-> ...

However, I already have the novel object so i would like to not look it up a second time.


回答1:


There is “Lazy Eager Loading“. The syntax is $novel->load('chapters', 'bookmarks');



来源:https://stackoverflow.com/questions/40406089/eager-loading-with-route-model-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!