<small id='cq9um'></small><noframes id='cq9um'>

  • <tfoot id='cq9um'></tfoot>

      <i id='cq9um'><tr id='cq9um'><dt id='cq9um'><q id='cq9um'><span id='cq9um'><b id='cq9um'><form id='cq9um'><ins id='cq9um'></ins><ul id='cq9um'></ul><sub id='cq9um'></sub></form><legend id='cq9um'></legend><bdo id='cq9um'><pre id='cq9um'><center id='cq9um'></center></pre></bdo></b><th id='cq9um'></th></span></q></dt></tr></i><div id='cq9um'><tfoot id='cq9um'></tfoot><dl id='cq9um'><fieldset id='cq9um'></fieldset></dl></div>

        • <bdo id='cq9um'></bdo><ul id='cq9um'></ul>
        <legend id='cq9um'><style id='cq9um'><dir id='cq9um'><q id='cq9um'></q></dir></style></legend>

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

        Check if name is unique among non-deleted items with laravel validation(使用 laravel 验证检查名称在未删除项目中是否唯一)

          <tbody id='solmH'></tbody>

      1. <i id='solmH'><tr id='solmH'><dt id='solmH'><q id='solmH'><span id='solmH'><b id='solmH'><form id='solmH'><ins id='solmH'></ins><ul id='solmH'></ul><sub id='solmH'></sub></form><legend id='solmH'></legend><bdo id='solmH'><pre id='solmH'><center id='solmH'></center></pre></bdo></b><th id='solmH'></th></span></q></dt></tr></i><div id='solmH'><tfoot id='solmH'></tfoot><dl id='solmH'><fieldset id='solmH'></fieldset></dl></div>
        <tfoot id='solmH'></tfoot>
            <bdo id='solmH'></bdo><ul id='solmH'></ul>
            <legend id='solmH'><style id='solmH'><dir id='solmH'><q id='solmH'></q></dir></style></legend>

            • <small id='solmH'></small><noframes id='solmH'>

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                    <tfoot id='5sk8O'></tfoot>
                      <tbody id='5sk8O'></tbody>

                          <bdo id='5sk8O'></bdo><ul id='5sk8O'></ul>
                          <legend id='5sk8O'><style id='5sk8O'><dir id='5sk8O'><q id='5sk8O'></q></dir></style></legend>

                            <small id='5sk8O'></small><noframes id='5sk8O'>

                            <i id='5sk8O'><tr id='5sk8O'><dt id='5sk8O'><q id='5sk8O'><span id='5sk8O'><b id='5sk8O'><form id='5sk8O'><ins id='5sk8O'></ins><ul id='5sk8O'></ul><sub id='5sk8O'></sub></form><legend id='5sk8O'></legend><bdo id='5sk8O'><pre id='5sk8O'><center id='5sk8O'></center></pre></bdo></b><th id='5sk8O'></th></span></q></dt></tr></i><div id='5sk8O'><tfoot id='5sk8O'></tfoot><dl id='5sk8O'><fieldset id='5sk8O'></fieldset></dl></div>