<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 验证检查名称在未删除项目中是否唯一

        时间:2023-10-31

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

                  上一篇:在 laravel 项目中生成动态站点地图而不使用作曲家 下一篇:Laravel 4 - 刀片模板 - 如何正确链接到路线?

                  相关文章

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

                        <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>