Laravel ORM特殊操作与like binary应用
掌握Laravel ORM中的特殊查询操作技巧。
以数据库采用ci(大小写不敏感)编码为例,若直接搜索"test",系统会返回Test、teST等所有变体。然而,若在查询条件中应用`like binary`,则仅能精准匹配完全一致的"test",该机制独立于MySQL的默认排序规则,确保结果严格区分大小写。
#passthru: array:10 [▼
0 => “insert”
1 => “insertGetId”
2 => “getBindings”
3 => “toSql”
4 => “exists”
5 => “count”
6 => “min”
7 => “max”
8 => “avg”
9 => “sum”
]
#operators: array:26 [▼
0 => “=”
1 => “<”
2 => “>”
3 => “<=”
4 => “>=”
5 => “<>”
6 => “!=”
7 => “like”
8 => “like binary”
9 => “not like”
10 => “between”
11 => “ilike”
12 => “&”
13 => “|”
14 => “^”
15 => “<<”
16 => “>>”
17 => “rlike”
18 => “regexp”
19 => “not regexp”
20 => “~”
21 => “~*”
22 => “!~”
23 => “!~*”
24 => “similar to”
25 => “not similar to”
]
相关参考文件位置:
D:\phpStudy\WWW\BCCAdminV1.0\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php
protected $bindings = [
'select' => [],
'join' => [],
'where' => [],
'having' => [],
'order' => [],
'union' => [],
];
protected $operators = [
'=', '<', '>', '<=', '>=', '<>', '!=',
'like', 'like binary', 'not like', 'between', 'ilike',
'&', '|', '^', '<<', '>>',
'rlike', 'regexp', 'not regexp',
'~', '~*', '!~', '!~*', 'similar to',
'not similar to',
];
public function index($customer_type = null) {
$search = request('search');
$perPage = request('perPage') ? request('perPage') : 10;
$customer_type = $customer_type ? $customer_type : request('customer_type');
$data = Customer::select(['id', 'email', 'user_name', 'nick_name', 'status', 'phone', 'create_time'])
->where('customer_type', '=', $customer_type)
->where(function ($query) use ($search) {
if ($search) {
$query->where('user_name', 'like binary', '%' . $search . '%')
->orWhere('nick_name', 'like binary', '%' . $search . '%')
->orWhere('phone', 'like binary', '%' . $search . '%')
->orWhere('email', 'like binary', '%' . $search . '%');
}
})
->orderBy('create_time', 'desc')
->paginate($perPage);
//追加额外参数,例如搜索条件
$appendData = $data->appends(array(
'search' => $search,
'perPage' => $perPage,
));
return view('admin/customer/customerList', compact('data'));
}
上述关于Laravel模糊查询区分大小写的实例内容至此分享完毕,希望能为读者提供有益参考,同时也欢迎继续支持本站获取更多技术资讯。