0

I have the following query:

Post::whereIn('id', [3, 4, 1, 2])->paginate(10);

And the result I get is in the following order (ids I mean):

1, 2, 3, 4 ...

Is it possible to somehow get the result in the order of how ids are passed to the query?

Bravi
  • 713
  • 2
  • 8
  • 29
  • Not answering your question, but just so you know, in case you don't know why this is happening: relational databases don't have the concept of "order". You don't get an ordered list of results, but a tuple (set) of results. – dabadaba May 08 '16 at 19:01
  • Yeah, I was thinking this is a mySQL thing, rather than Laravel.. But perhaps there is a solution.. – Bravi May 08 '16 at 19:02
  • Check [this](http://stackoverflow.com/questions/26704575/laravel-order-by-where-in) out, does it help? – dabadaba May 08 '16 at 19:06

2 Answers2

0

Probabyl you need to sort then in the query like this:

Post::whereIn('id', [3, 4, 1, 2])->orderBy('id')->paginate(10);
Filip Koblański
  • 9,718
  • 4
  • 31
  • 36
0

read laravel doc from this link for whereIn() you didnt make any objects of your POST model make an object and work with it, its better this way

https://laravel.com/docs/5.2/collections#method-wherein

hope
  • 27
  • 7
  • The problem is that I have to work with Query Builder, not the collection. Because the collection cannot be paginated and my project uses Dingo API and expects a query builder paginator.. – Bravi May 08 '16 at 19:47