ahmedalahmed's avatar

override pagination data

How to override pagination data like :


$products = Product::where('id', '>', 20)->select('id')->paginate(10);
/*  Now i need to replace each item in data with new one 
after adding some custom fields  */

0 likes
8 replies
Tray2's avatar

What do you mean with override?

The code you have selects 10 rows that have the id greater than 20 and returns the id.

If you want to get all the fields remove the ->select()

ahmedalahmed's avatar

i mean the following : suppose i need to return an unified form for the product like with some other relationships so my idea was create a function with an id as parameter and return the desired form of the given id from product table. so by using pagination i need just IDs from and for each one i will invoke the previous function to return my form.

Tray2's avatar

Sounds more complicated than neccesary.

jlrdw's avatar

Why override, just re-paginate as needed.

jlrdw's avatar
jlrdw
Best Answer
Level 75

like with some other relationships

Have controller method for that and paginate your new stuff. Don't try to override.

You may be able to set up query scopes for this, scopes are covered well in the documentation.

https://laravel.com/docs/5.6/eloquent#query-scopes

2 likes

Please or to participate in this conversation.