How to add collate to laravel query

前端 未结 3 1084
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 15:59

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

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

This is

3条回答
  •  囚心锁ツ
    2021-01-18 16:30

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

提交回复
热议问题