dongpeng7744 2016-04-12 13:19 采纳率: 100%
浏览 15
已采纳

如果id相同,Laravel验证唯一

I have a table/model that contains multiple albums per user. Is there a way to say that the column title should be unique, but only for the rows that have the same user_id?

Example: http://pastebin.com/8dvM4a1T

As you can see in the example, the user with the id of 2 has created 2 albums, with the same title. I don't want that to be allowed, that's why I'm wondering if there's a way to deny that with the validator from Laravel?

I tried this, but that did not work.

// Validator
    $validator = Validator::make($input, [
        'title' => 'required|min:1|max:255|unique:galleries,title,'. Auth::user() -> id .',user_id',
        'description' => 'min:1|max:255'
    ]);

Any help appreciated, thanks.

  • 写回答

2条回答 默认 最新

  • doutany76678 2016-04-12 13:49
    关注

    The approach with the default unique rule does not work because the rule expects the column value to be passed as the third parameter, so in your case it would check if the title column is equal to the Auth::user()->id value which is not what you want.

    You can create you own custom validation rule by adding the following code to the boot method of the App\Providers\AppServiceProvider class:

    Validator::extend('unique_custom', function ($attribute, $value, $parameters)
    {
        // Get the parameters passed to the rule
        list($table, $field, $field2, $field2Value) = $parameters;
    
        // Check the table and return true only if there are no entries matching
        // both the first field name and the user input value as well as
        // the second field name and the second field value
        return DB::table($table)->where($field, $value)->where($field2, $field2Value)->count() == 0;
    });
    

    Now you can use the unique_custom (or you can name it whatever you like) rule like so:

    $validator = Validator::make($input, [
        'title' => 'required|min:1|max:255|unique_custom:galleries,title,user_id,' . Auth::id(),
        'description' => 'min:1|max:255'
    ]);
    

    The rule expects the parameters to be the following:

    • the 1st parameter to be the table name, which in this case is galleries
    • the 2nd parameter to be the field name that is supposed to be unique and for which the value comes from the user input, which in this case is title
    • the 3rd parameter to be the second field name that will be added to the query conditions, which in this case is user_id
    • the 4th parameter to be the value of the field name passed as the third parameter

    Also you can use Auth::id() since that is the short form of Auth::user()->id.


    You can read more about Custom Validation rules in the Laravel Documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序