Laravel 查询构建器中的 mysql 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26465243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql functions in query builder in laravel
提问by TigerWhite
I want to use the function in MySQL like convert(name use gbk)
.
我想在 MySQL 中使用该函数,例如convert(name use gbk)
.
How can I use this with Laravel's query builder with
?
如何在 Laravel 的查询构建器中使用它with
?
I tried ->orderBy(convert(name using gbk))
but it doesnt work.
我试过了,->orderBy(convert(name using gbk))
但它不起作用。
回答by Needpoule
You need to use the Raw function of eloquent.
需要用到eloquent的Raw功能。
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.
如果您想在 where 语句中使用原始 sql,您可以使用快捷功能whereRaw()
并选择该selectRaw()
功能。