验证Laravel中的唯一名称和名字

编程入门 行业动态 更新时间:2024-10-18 06:09:06
本文介绍了验证Laravel中的唯一名称和名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这个问题存在,但是到目前为止我还没有找到解决方案.我有一个名为 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中的唯一名称和名字

本文发布于:2023-11-08 20:19:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570355.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名字   名称   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!