Search code examples
phpvalidationconditional-statementslaravel-4unique

Laravel 4: Unique Validation with Extra Condition While Updating Record


I am soft deleting events in the database and while updating any event i need to check if user replace the value of "sysname" field with some existing (non deleted and active) event's sysname then i need to prevent it. Therefore, i just want to know that while applying unique validation for the relevant field, how would i assign a condition to check for is_deleted = 0 and status=1

Here is my code of Event model:

public static function editrules($id=0, $merge=[]) {
    return array_merge(
        [
            'sysname' => 'required|unique:tblEvent,sysname,'.$id.',event_id',
        ], 
        $merge);
}

The above code simply prevent to update existing event "sysname" for the current event. But i need to know that what code i need to add in above sample code to check existing non deleted (is_deleted = 0) and active event (status=1)


Solution

  • "unique:tblEvent,sysname,{$id},event_id,deleted_at,NULL,status,1"