Laravel unique validation on multiple columns

前端 未结 8 1664
天涯浪人
天涯浪人 2020-11-27 05:01

I have 2 columns in table servers.

I have columns ip and hostname.

I have validation:

\'data.ip\' => [\'required         


        
8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 05:30

    The following will work on the create

    'data.ip' => ['required', 'unique:servers,ip,'.$this->id.',NULL,id,hostname,'.$request->input('hostname')]
    

    and the following for the update

    'data.ip' => ['required', 'unique:servers,ip,'.$this->id.','.$request->input('id').',id,hostname,'.$request->input('hostname')]
    

    I'm presuming that id is your primary key in the table. Substitute it for your environment.


    The (undocumented) format for the unique rule is:

    table[,column[,ignore value[,ignore column[,where column,where value]...]]]

    Multiple "where" conditions can be specified, but only equality can be checked. A closure (as in the accepted answer) is needed for any other comparisons.

提交回复
热议问题