lundi 10 décembre 2018

Making new custom relation method in laravel model

I am using this for my api when i visit someone profile:

return response()->
json(
    User::load(["posts.comments"])
    ->where('username', request('username'))
    ->firstOrFail()
);

Now when I want to use paginate on relation I do it like this:

$user->posts()->paginate(10);

But I need relation so I found:

$user->setRelation('posts', $user->posts()->paginate(10));

Now I am wondering is there a way to create custom relation method inside model so when I use load(['posts.comments']) it returns that custom relations?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire