如何添加整理到laravel查询 [英] How to add collate to laravel query

查看:116
本文介绍了如何添加整理到laravel查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要运行一个具有 collat​​e utf8_bin 的查询,如下所示:

I need to run a query having collate utf8_bin like so:

SELECT * FROM `table` WHERE `field`='value' collate utf8_bin;

这是严格用于管理脚本,我不想更新表charset本身,只是对于特定的查询。

This is strictly for an admin script and I don't want to update the table charset itself, just for the particular query.

我可以使用Eloquent ORM做这个,还是需要写出这个查询?

Can I do this using the Eloquent ORM or do I need to write this query out?

推荐答案

由于您可以配置MySQL驱动程序使用一个:

Since you can configure MySQL driver to use one:

'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(...);

编辑:

在模型中,您可能可以通过以下方式设置连接:

On a Model, you probably will be able to set a connection this way:

$posts = new Word;
$posts->setConnection('mysql-collation');
$posts->where(...);

这篇关于如何添加整理到laravel查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆