2
votes

Is it possible to use the following orderByRaw(). This orderByRaw() is returning the products in the same order as the whereIn(). Right now it's only the results in the orderByRaw order and not the orderBy().

Is it possibile to combine these:

$ids;
$placeholders = implode(',',array_fill(0, count($ids), '?'));
Product::whereIn('id', $ids)->orderByRaw("field(id,{$placeholders})" ,$ids)->orderBy('views', 'des')->orderBy('created_at', 'des')->get();

Edit: This is the SQL I get when I do toSql(). Why isn't this working?

SELECT * 
FROM   `products` 
WHERE  `id` IN ( ?, ?, ?, ?, ? ) 
ORDER  BY Field(id, ?, ?, ?, ?, ?), 
          `views` DESC, 
          `created_at` DESC 
2
I suppose you want to write 'desc' and not 'des'. This may be the issue.Oluwatobi Samuel Omisakin

2 Answers

1
votes

Let’s imagine a simple situation – you need to write an Eloquent query and order the result by the difference of two columns. Or some other calculation. Time to apply a raw query!

Simple orderBy:

User::where('created_at', '>', '2016-01-01')
  ->orderBy('updated_at', 'desc')
  ->get();

Now, what if we need to order by the difference between updated_at and created_at? It looks like this:

User::where('created_at', '>', '2016-01-01')
  ->orderByRaw('(updated_at - created_at) desc')
  ->get();

This orderByRaw() method, which is not mentioned in official Laravel documentation, will apply order by clause without changing anything in it, so final query will be:

select * from users 
  where created_at > '2016-01-01' 
  order by (updated_at - created_at) desc

update my answer replace this

->orderBy('views', 'des')->orderBy('created_at', 'des')->get()

to

 ->orderBy('views', 'desc')->orderBy('created_at', 'desc')->get()

Hope that helps!

0
votes

Yes, you can do that. Each orderByRaw and orderBy clause returns the query builder, so you can chain off of them. If you output ->toSql() at the end, you can see the generated output.