The usage of laravel unique is "'email' => ['required', 'email',Rule::unique('customer')->...})]," which means update When checking whether the mailbox is duplicated, exclude yourself and all users with status 4.
The operating environment of this tutorial: Windows 7 system, Laravel version 5.5, DELL G3 computer.
Laravel unique rule usage
Laravel’s unique usage:
$customer_type = request('customer_type', 1); $attributes = request()->validate([ 'gender' => 'required|min:0|max:3', 'chinese_name' => 'nullable|min:2', 'english_name' => 'nullable|min:3', 'area_phone_number' => 'required|numeric', 'telephone' => 'required|numeric', 'email' => ['required', 'email', Rule::unique('customer')->ignore(request('id'))->where(function ($query) { $query->whereNotIn('status', [4]); })], // 'email' => 'required|email|unique:customer,email,' . request('id'), 'password' => 'nullable|string|min:6', 'concurrent_login_num' => 'required|min:1|integer', 'child_age' => 'nullable|integer|min:0', 'remark' => 'nullable|string|max:1024', 'status' => 'required|integer|min:0|max:4', ]);
Pay attention to this usage:
'email' => ['required', 'email', Rule::unique('customer')->ignore(request('id'))->where(function ($query) { $query->whereNotIn('status', [4]); })],
This means When updating information to detect whether mailboxes are duplicated, exclude yourself and all users with status 4. In my case, users with status 4 are deleted. If this mailbox is used by a deleted user, then the duplication is not checked.
参考资料:https://laravel.com/docs/5.5/validation 文件位置: D:\phpStudy\WWW\BCCKidAdmin\vendor\laravel\framework\src\Illuminate\Validation\Rules\DatabaseRule.php 关于各种的的验证: D:\phpStudy\WWW\BCCKidAdmin\vendor\laravel\framework\src\Illuminate\Validation\Concerns\ValidatesAttributes.php D:\phpStudy\WWW\BCCKidAdmin\vendor\laravel\framework\src\Illuminate\Validation\Concerns\ReplacesAttributes.php
Related recommendations: The latest five Laravel video tutorials
The above is the detailed content of What is the usage of laravel unique. For more information, please follow other related articles on the PHP Chinese website!