0

I want to convert this SQL query to Query builder Laravel

SELECT * FROM articles ORDER BY (titre LIKE '%book%') DESC

1 Answer 1

1

Updated: Try the following code:

DB::table('articles')->orderBy(DB::raw("title LIKE '%$value%'"),'desc')->get();

I think the following one also solves your problem:

DB::table('articles')->orderByRaw("(title LIKE '%book%') DESC")->get();
Sign up to request clarification or add additional context in comments.

6 Comments

great, with variable : orderBy(DB::raw("title LIKE '%$value%'"),'desc') thanks
Thanks. If it works, please accept it as correct answer.
pls, how do I do to add a second field 'description' : DB::table('articles')->orderByRaw("(title, description LIKE '%book%') DESC")->get(); not work!
You may use multiple orderBy functions as below: DB::table('articles')->orderByRaw("(title LIKE '%book%') DESC, (description LIKE '%book%') DESC")->get();
I fixed the problem, it had another table with the description field, so i have to add the name of the table with the description field : articles.description thanks again
|

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.