9

I want to use the function in MySQL like convert(name use gbk).

How can I use this with Laravel's query builder with?

I tried ->orderBy(convert(name using gbk)) but it doesnt work.

1 Answer 1

20

You need to use the Raw function of eloquent.

DB::raw(your sql)

In your case, the following query should work:

->orderBy(DB::raw('convert(name using gbk)'))

If you want to use raw sql in your where statements, your can use the shortcut function whereRaw() and for a select the selectRaw() function.

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

3 Comments

By the way, it can be also used as: ->where('t','<=',DB::raw('now()'));
I have just used this answer with COALESCE(parent_aid, aid), parent_aid` IS NOT NULL` by just placing DB::raw() in orderBy(). It works perfect!
It works with insert as well: $id = DB::table('mytable')->insertGetId(array('uuid' => DB::raw('uuid()'));

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.