Laravel验证:存在两列相同的行

编程入门 行业动态 更新时间:2024-10-26 20:32:55
本文介绍了Laravel验证:存在两列相同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Laravel中指定存在的验证规则时,是否可以引用另一个字段?

Is there a way of referencing another field when specifying the exists validation rule in Laravel?

我的请求:

public function rules() { return [ 'numero_de_somme' => 'unique:personnels,numero_de_somme|exists:fonctionnaire,num_somme', 'cin' => 'unique:personnels,cin|exists:fonctionnaire,cin', ]; }

在我的验证规则中,我希望能够确保:

in my validation rules I want to be able to make sure that:

  • num_somme 存在于 fonctionnaire 表中
  • 功能表中存在
  • cin ,并且 cin 输入必须为在 num_somme
  • 的同一行上
  • num_somme exists within the fonctionnaire table
  • cin exists within the fonctionnaire table and cin input must be on the same row of the num_somme

num_somme:12345cin:s89745

num_somme : 12345 cin : s89745

num_somme:78945电影:U10125

num_somme : 78945 cin : U10125

解释:例如

  • 第一种情况,如果输入 num_somme = 12345并且 cin = U10125,则验证必须失败
  • 第二种情况,如果输入 num_somme = 12345并且 cin = s89745,则验证必须成功
  • 1st scenario if the input num_somme = 12345 and cin = U10125 the validation must fail
  • 2nd scenario if the input num_somme = 12345 and cin = s89745 the validation must success

我希望这是有道理的.

谢谢

推荐答案

我今天遇到了同样的需求,我认为我有使用验证规则类的解决方案:规则示例.

I came across the same need today and I think I have a solution using the validation Rule class: Rule example.

这是我的情况:我有一个电子邮件验证表,我想确保传递的机器代码和激活代码在同一行中.

Here is my scenario: I have an email verifications table and I want to ensure that a passed machine code and activation code exist on the same row.

请确保包含 use Illuminate \ Validation \ Rule;

$activationCode = $request->activation_code; $rules = [ 'mc' => [ 'required', Rule::exists('email_verifications', 'machineCode') ->where(function ($query) use ($activationCode) { $query->where('activationCode', $activationCode); }), ], 'activation_code' => 'required|integer|min:5', 'operating_system' => 'required|alpha_num|max:45' ];

exists方法中的第一个参数是表格,第二个参数是我用于"mc"字段的自定义列名称.我使用"use"关键字传递要检查的第二列,然后在where子句中使用该字段.

The first argument in the exists method is the table and the second is the custom column name I'm using for the 'mc' field. I pass the second column I want to check using the 'use' keyword and then use that field in a a where clause.

更多推荐

Laravel验证:存在两列相同的行

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

发布评论

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

>www.elefans.com

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