How to add collate to laravel query

前端 未结 3 1108
没有蜡笔的小新
没有蜡笔的小新 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:08

    If you only need to apply this to the values in the WHERE clause:

    $value = "àBc123";
    $query->whereRaw('field_name COLLATE utf8mb4_bin = (?)', $value);
    
    • This query is both case sensitive and accent sensitive.
    • Replace utf8mb4_bin with whatever collation makes sense on your system (eg. utf8_bin).
    • Using (?) should utilize Laravel's query sanitization.

    For accent sensitive but NOT case sensitive:

    $value = "àBc123";
    $query->whereRaw('LOWER(field_name) COLLATE utf8mb4_bin = (?)', strtolower($value));
    

提交回复
热议问题