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

时间:2014-04-29 20:51:03

标签: php validation laravel laravel-4

我有一个简单的表单,发布到控制器,该控制器检查是否已为特定项目获取项目的名称。如果是,则返回错误。这是我用过的代码:

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

我遇到的问题是,我使用软删除功能将其从数据库中删除,而不是硬删除,这意味着,例如,'测试'只有在名称被删除后才能用作名称。

如何在非软删除的项目中检查该项目是否唯一?

5 个答案:

答案 0 :(得分:28)

你可以试试这个:

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

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

要在更新时忽略模型,您应该在id之后的name传递NULL

更新:您也可以使用类似的内容添加自己的自定义规则:

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

答案 1 :(得分:11)

我知道这个问题很老,但在寻找类似问题的解决方案时,我偶然发现了这个问题。您不需要自定义验证码。

我有一个分类帐代码数据库,其中'name'和'short_name'对每个用户(user_id)必须是唯一的。我启用了软删除功能,因此它们在给定用户的非删除行中只能是唯一的。

这是我的函数,它返回验证字符串:

protected function validation_data($user_id, $update_id = "NULL") {
        return [
        'name' => "required|max:255|unique:ledger_codes,name,$update_id,id,deleted_at,NULL,user_id,$user_id",
        'short_name' => "max:255|min:3|unique:ledger_codes,short_name,$update_id,id,deleted_at,NULL,user_id,$user_id",
        'description' => 'max:255',
        ];
    }

对于任何想知道唯一验证器的参数字符串语法的人,它如下:

arg 0: The table name in the database
arg 1: the field name in which the value is unique
arg 2: a single id which is to be ignored (set to uppercase NULL if you are not using it.)
arg 3: the field in which the single id resides.  It defaults to 'id' so if you are not using it, and you have more arguments, use the string 'id'.
arg 4/5:  a field name/value pair for a where clause (where('deleted_at',null) in my example.)
arg 6/7: another field name/value pair (where('user_id', $user_id) in my example).
arg 8/9: another field name value pair
arg 10/11: another.......
... and so on. 

字段名称/值对中的值字段可以是要匹配的值,NULL或NOT_NULL。

答案 2 :(得分:5)

如果有人正在使用Rule类来寻找解决方案。

use Illuminate\Validation\Rule;

class UpdateArticleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $data = $this->request->all();

        return [
            'slug' => [
                'required',                
                Rule::unique('articles')->ignore($data['id'])->whereNull('deleted_at')
            ]
        ];
    }
}

基本上,我们只是忽略deleted_at字段不是null的行。

以下是您可以与ignore函数一起使用的方法:https://laravel.com/api/5.7/Illuminate/Validation/Rules/DatabaseRule.html

答案 3 :(得分:2)

添加记录

'name' => [
                'required',
                'string',
                'min:3',
                Rule::unique('products')->where(function ($query) {
                    return $query->where('store_id', Auth::user()->store_id);
                })->whereNull('deleted_at'),                
            ],

用于编辑该记录

'name' => [
                'required',
                'string',
                'min:3',
                Rule::unique('products')->where(function ($query) {
                    return $query->where('store_id', Auth::user()->store_id);
                })->ignore($request->id, 'id')->whereNull('deleted_at'),                
            ],

答案 4 :(得分:1)

关于创建方法:

public function store(Request $request)
{

    $request->validate([

        'name'=>'required|unique:form_types,name,NULL,id,deleted_at,NULL',

    ]);

    // Write your code here

}

关于更新方法:

public function update(Request $request, $id)
{

    $request->validate([

        'name'=>'required|unique:form_types,name,'.$id.',id,deleted_at,NULL',

    ]);

    // Write Code here

}