Laravel 4: unique check for multiple columns

I know this question has been asked before, but I have not received a corresponding answer.

I want to know how to write a rule for checking the uniqueness of two columns. I tried to write a rule like:

public $rules = array(
    "event_id"=>"required",
    "label"=>"required|unique:tblSection,label,event_id,$this->event_id",
    "description"=>"required"
);

In my example, I need to check to ensure that one label can be unique for one event identifier, but can be used for another event identifier. For example, I want to achieve:

id   event_id    label   description
1     1          demo    testing
2     2          demo    testing

In the rule defined above, I somehow need to pass the currently selected event_id so that it can check if a label exists in the database table for the selected event_id event, but I get a syntax error, for example:

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"syntax error, unexpected '\"'","file":"\/var\/www\/tamvote\/app\/modules\/sections\/models\/Sections.php","line":39}}

. - , , laravel 4, .

+4
4

.

store:

Model::$rules['label'] = 'required|unique:table_name,label,NULL,event_id,event_id,' .$data['event_id'];

$data - POST.

update:

$model = Model::find($id);
Model::$rules['label'] = 'required|unique:table_name,label,NULL,event_id,event_id,'.$data['event_id'].',id,id'.$model->id;

$data - PUT/PATCH, $model - , , id - .

+4

, , event_Id , :

'label' => 'unique:table_name,label,NULL,event_id,event_id,'.$eventId
//you should get the $eventId first
+2

, . , .

protected $rules = [
    'user_id' => 'unique_multiple:memberships,user_id,group_id',
    'group_id' => 'unique_multiple:memberships,user_id,group_id',
]

/**
 * Validates that two or more fields are unique
 */
Validator::extend('unique_multiple', function ($attribute, $value, $parameters, $validator)
{
    //if this is for an update then don't validate
    //todo: this might be an issue if we allow people to "update" one of the columns..but currently these are getting set on create only
    if (isset($validator->getData()['id'])) return true;

    // Get table name from first parameter
    $table = array_shift($parameters);

    // Build the query
    $query = DB::table($table);

    // Add the field conditions
    foreach ($parameters as $i => $field){
        $query->where($field, $validator->getData()[$field]);
    }

    // Validation result will be false if any rows match the combination
    return ($query->count() == 0);

});
+2

, .

Validator::extend('uniqueEventLabel', function ($attribute, $value, $parameters, $validator) {
    $count = DB::table('table_name')->where('event_id', $value)
                                    ->where('label', $parameters[0])
                                    ->count();

    return $count === 0;
}, 'Your error message if validation fails.');

, :

'event_id' => "uniqueEventLabel:".request("label")

, where sql.

(: edcs )

0

Source: https://habr.com/ru/post/1545032/


All Articles