如何将整理添加到 Laravel 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19514247/
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
How to add collate to laravel query
提问by Rob
I need to run a query having collate utf8_bin
like so:
我需要运行这样的查询collate utf8_bin
:
SELECT * FROM `table` WHERE `field`='value' collate utf8_bin;
This is strictly for an admin script and I don't want to update the table charset itself, just for the particular query.
这是严格的管理脚本,我不想更新表字符集本身,只是为了特定的查询。
Can I do this using the Eloquent ORM or do I need to write this query out?
我可以使用 Eloquent ORM 来做到这一点还是我需要写出这个查询?
回答by ddn
You can do it this way if it solves your problem:
如果它解决了您的问题,您可以这样做:
SomeModel::whereField($value)->orderByRaw("name COLLATE utf8_bin ASC")->get();
回答by Antonio Carlos Ribeiro
Since you can configure MySQL driver to use one:
由于您可以将 MySQL 驱动程序配置为使用一个:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
You can create a different connection for your particular query:
您可以为您的特定查询创建不同的连接:
'mysql-collation' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => '<your collation>',
'prefix' => '',
),
And use that connection on your query:
并在您的查询中使用该连接:
$users = DB::connection('mysql-collation')->select(...);
EDIT:
编辑:
On a Model, you probably will be able to set a connection this way:
在模型上,您可能可以通过以下方式设置连接:
$posts = new Word;
$posts->setConnection('mysql-collation');
$posts->where(...);