使用 laravel 验证检查名称在未删除项目中是否唯一

2023-10-31php开发问题
4

本文介绍了使用 laravel 验证检查名称在未删除项目中是否唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个简单的表单,它发布到一个控制器,该控制器检查一个项目的名称是否已经用于特定项目.如果是,则返回错误.这是我正在使用的代码:

I have a simple form which posts to a controller which checks if a name for an item is already taken for a particular project. If it is, then it returns an error. This is the code I'm using for that:

'name'    => 'required|min:1|unique:versions,name,NULL,id,project_id,'.$project->id,

我遇到的问题是,我没有使用硬删除,而是使用软删除将它们从数据库中删除,这意味着,例如,Test"只能用作名称一次,即使它已被删除.

The problem I've run into is that instead of a hard delete, I'm using a soft delete to remove them from the database, meaning that, for example, 'Test' can only be used as the name once, even after it's been deleted.

我怎样才能让它在未软删除的项目中检查该项目的唯一性?

How can I make it check that it is unique for that project among the items that are not soft deleted?

推荐答案

你可以试试这个:

'name' => 'required|min:1|unique:versions,name,NULL,id,deleted_at,NULL'

这将确保 versions 表中的 name 是唯一的,如果一条记录被软删除并且具有相同的名称 name 则不会被计算在内, 表示即使存在同名的软删除记录,名称也会被接受.

This will make sure that the name in the versions table will be unique, if a record is soft deleted and has same name name then it won't be counted, means, name will be accepted even if there is a soft deleted record with the same name exists.

要在更新时忽略模型,您应该在 name 之后传递 id 来代替第一个 NULL.

To ignore a model when updating, you should pass the id after name in the place of first NULL.

更新:你也可以使用这样的东西来添加你自己的自定义规则:

Update: Also you may use something like this to add your own custom rule:

// You can declare it inside your controller method before you run validation
Validator::extend('unique_project', function($attribute, $value, $parameters)
{
   // $attribute will contain field name, i.e. name
   // $value will contain the value in the $attribute/name
   // $parameters will be an array of arguments passed
   // i.e. [0] => arg1, [1] => arg2, [2] => arg3 and so on

   return true for valid and false for invalid

});

你可以这样使用它:

'name' => 'required|min:1|unique_project:arg1,arg2,arg3' // add more args if needed

这篇关于使用 laravel 验证检查名称在未删除项目中是否唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17