[英]Making new custom relation method in laravel model
訪問某人的個人資料時,我將其用於我的api:
return response()->
json(
User::load(["posts.comments"])
->where('username', request('username'))
->firstOrFail()
);
現在,當我想在關系上使用分頁時,我會像這樣:
$user->posts()->paginate(10);
但是我需要聯系,所以我發現:
$user->setRelation('posts', $user->posts()->paginate(10));
現在我想知道是否有一種方法可以在模型內部創建自定義關系方法,因此當我使用load(['posts.comments'])
它會返回該自定義關系嗎?
一種方法是執行此操作:
return response()->
json(
User::with(['posts' => function($q) {
$q->paginate(5);
}, 'posts.comments' => function($q) {
$q->paginate(5, ['*'], 'commentsPage', request('commentsPage'));
}])
->where('username', request('username'))
->firstOrFail()
);
雖然我不確定如何傳遞$q->nextPageUrl()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.