我知道这个问题存在,但是到目前为止我还没有找到解决方案.我有一个名为 players 的表,该表具有两个字段: name, firstname..我的目标是在输入多个条目时不重复.
I know the subject exists, but I haven't found a solution so far. I have a table which is named players with two fields: name, firstname. My goal is to have neither duplicate when I enter several entries.
我想得到这个结果.
Fostier | Alain Fostier | Jeremy如果我有两次福斯捷|阿兰,这是不正确的.
If I have two times Fostier | Alain it's not correct.
重复系统应仅合并ID?我尝试了以下方法,但没有成功.
The duplicate system should combine only the ID? I have tried the following without success.
public function store(Request $request) { $request->validate([ 'name' => 'required|unique:players,name', 'firstname' => 'required|unique:players,firstname', ]); Player::create($request->all()); flashy('Valider'); return redirect()->route('players.index') ->with('success', 'save'); } 推荐答案您可以使用 Illuminate \ Validation \ Rule 类进行复合检查,例如:
You can use the Illuminate\Validation\Rule class to make compound check e.g:
use Illuminate\Validation\Rule; ... $request->validate([ 'name' => ['required', Rule::unique('players', 'name')->where(function ($query) use ($request) { return $query->where('name','!=', $request->input('firstname')); })], 'firstname' => ['required', Rule::unique('players', 'firstname')->where(function ($query) use ($request) { return $query->where('firstname', '!=', $request->input('name')); })], ]);这还将通过检查是否使用了名称"来检查名字"的唯一性,并且对名称"也进行同样的检查.
This checks uniqueness of the 'firstname' by also checking if the 'name' is in use, and also does the same for 'name' checking the 'firstname' with it.
更多推荐
验证Laravel中的唯一名称和名字
发布评论