N
N
NubasLol2018-12-14 18:42:27
Laravel
NubasLol, 2018-12-14 18:42:27

Laravel how to make pagination of loaded links be given to resources?

Is it possible to give pagination of the reviews model to resources?

$model->with(['reviews' => function($query){
            return $query->paginate();
        }]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2018-12-14
@NubasLol

You can't do that. ->with() is the magic that takes care of how to get the data out. You specify the necessary conditions in the callback, but then ->with() calls ->get() and resolves everything itself. In addition, get returns an EloquentCollection, on which all further work with relations is based, and with your actions you are trying to slip a pagination object into it. Obviously this won't work.
Your variable is called model, so I assumed there was only one. In this case, this option will work:
If you need to do this for several models at once, then this is done only by separate requests. ->with() will not help there either, only the above code in the loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question