I have a list of ids that come from a complicated query. I paginate the response of that complicated query and then use those ids to get the eloquent models. I then put it through a resource with the pagination meta data.
The laravel AbstractPaginator
class protects the items
attribute so you cannot easily overwrite them. I have a solution to use a ReflectionProperty
but I'm after a simpler solution.
// $studentIds == Long complicated query that would return 1000s of students
$data = $studentIds->paginate(); // Execute the query limited to 15.
// Use ids to get eloquent models for our students
$students = Student::whereIn('id', $data->pluck('id'));
// Overwrite paginate `items` attribute so that our response contains pagination meta.
$rp = new \ReflectionProperty('Illuminate\Pagination\AbstractPaginator', 'items');
$rp->setAccessible(true);
$rp->setValue($data, $students);
return new StudentResourceCollection($data);
The above works but it is not particularly elegant, I'm after a better solution.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2Hpd0tq
via IFTTT
Aucun commentaire:
Enregistrer un commentaire