Laravel 雄辩条件在where条件下未检测重音字母
在我的web应用程序中,我有位置,每个位置都有Laravel 雄辩条件在where条件下未检测重音字母,laravel,Laravel,在我的web应用程序中,我有位置,每个位置都有公司id,我不希望在同一家公司中有相同的位置名称。所以我做了这个: $name = $request->get('name'); $location = Location::find($id); if ($location->name != $name) { $old_location = Location::where('company_id', $location->company_id) ->wh
公司id
,我不希望在同一家公司中有相同的位置名称。所以我做了这个:
$name = $request->get('name');
$location = Location::find($id);
if ($location->name != $name) {
$old_location = Location::where('company_id', $location->company_id)
->where('name', $name)
->first();
if ($old_location) {
$errors = ['name' => trans('asset.location').' '.$name.'
'.trans('message.already_exists')];
return back()->withErrors($errors)->withInput();
}
}
问题是,如果我有
服务
位置,并且想将其名称更改为服务
,它将不允许我这样做。我没有这样命名的位置。若条件通过,但在雄辩的搜索中失败。这是怎么可能的?确保在配置文件中正确指定了字符集。像
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
src:
和
我想,你必须在这里使用laravel验证。因此,它可以在处理之前验证请求
Validator::extend('uniqueLocationInCompany', function ($attribute, $value, $parameters, $validator) {
$count = DB::table('locations')->where('name', $value)
->where('company_id', $parameters[0])
->count();
return $count === 0;
});
然后,您可以在validator函数中访问此新规则:
'name' => "uniqueLocationInCompany:{$request->company_id}"
要了解我们应该如何在laravel中实现验证,您应该遵循此处的文档
我找到了!在阅读了上面的答案后,我将$old_location
的代码更改为:
$old_location = Location::where('company_id', $location->company_id)
->whereRaw('name COLLATE utf8mb4_bin', $name)
->first();
我对我雄辩的where条件使用了不同的排序规则,它工作得非常完美。charset就是这样,问题在于排序,我提供了答案。我可以把Validatior::extend代码放在哪里?在哪个文件中?在App\Providers\AppServiceProvider>boot()@Nemanja中,如果您认为这有助于解决您的问题,请标记此项。我刚刚做了,它确实帮助我找到了解决方案!