4

I am wondering if it is possible to customize the order of returned objects in a paginated result from a simple query. The response order now is pagination info followed by the "data" object which contains the model I have searched for. I was wondering if its possible to reverse the order so its product info followed by some sort of pagination object.

My query is:

return Product::paginate(100);

My result is:

{"total":466,"per_page":100,"current_page":1,"last_page":5,"from":1,"to":100,"data":[{"id":28,"account_id":5,"created_by_id":14,"type":"accessory","title":"Product #28","description":"D","max_per_order":1,"stock":3565,"created_at":"1998-04-18 00:00:00","updated_at":"1994-10-28 00:00:00","deleted_at":null},{"id":44,"account_id":5,"created_by_id":36,"type":"registration","title":"Product #44","description":"Z","max_per_order":10,"stock":13739,"created_at":"2014-04-20 00:00:00","updated_at":"1991-06-03 00:00:00","deleted_at":null}]}

What I would like the result to be would be something like this:

[products: [{"id":28,"account_id":5,"created_by_id":14,"type":"accessory","title":"Product #28","description":"D","max_per_order":1,"stock":3565,"created_at":"1998-04-18 00:00:00","updated_at":"1994-10-28 00:00:00","deleted_at":null},{"id":44,"account_id":5,"created_by_id":36,"type":"registration","title":"Product #44","description":"Z","max_per_order":10,"stock":13739,"created_at":"2014-04-20 00:00:00","updated_at":"1991-06-03 00:00:00","deleted_at":null},{"id":57,"account_id":5,"created_by_id":40,"type":"accessory","title":"Product #57","description":"Z","max_per_order":4,"stock":19376,"created_at":"1970-03-27 00:00:00","updated_at":"2001-01-01 00:00:00","deleted_at":null}], pagination: {"total":466,"per_page":100,"current_page":1,"last_page":5,"from":1,"to":100}]

How would I go about doing something like this? Would it be easier to write my own pagination code?

2 Answers 2

13

There's no way as such using laravel specific measure to do this, it'll require you actually grabbing the data and creating your own custom array to be returned as JSON. It is however, pretty easy, I've added an example below.

$products = Product::paginate(100);
$response = [
    'products'   => $products->getItems()->toArray(),
    'pagination' => [
        'total'        => $products->getTotal(),
        'per_page'     => $products->getPerPage(),
        'current_page' => $products->getCurrentPage(),
        'last_page'    => $products->getLastPage(),
        'from'         => $products->getFrom(),
        'to'           => $products->getTo()
    ]
];

return Response::json($response);

I've recently created several APIs with laravel, and one of the problems I faced was paginating data while keeping a uniform response, this is how I handled it.

Hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

That's your PHP version, [] is shorthand for arrays in php 5.4+ iirc.
Then I don't think you're using 5.5, afaik, there isn't a configuration option for it.
@Irfan by being on the second page, ergo, the same page but with ?page=2
0

Laravel already included this feature, try this

$products = Product::paginate(100)->toJson();
return $products

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.