2
votes

In my Laravel-5.8 project I am implementing Rules Request validation.

use SoftDeletes;

Model: AppraisalGoal (appraisal_goals)

protected $fillable = [
              'id',
              'goal_type_id',
              'appraisal_identity_id',
              'employee_id',
              'deleted_at',
          ]; 

protected $dates = [
    'deleted_at',
];

Rules Request: create

public function rules()
{
        'goal_type_id' => [
            'required', 
            Rule::unique('appraisal_goals')->where(function ($query) {
           return $query->where('appraisal_identity_id', $this->appraisal_identity_id)
                ->where('goal_type_id', $this->goal_type_id)
              ->where('employee_id', $this->employee_id);
        })               
        ],
       
    ];
} 

Rules Request:edit

public function rules()
{
        'goal_type_id' => 
        [
            'required',
            Rule::unique('appraisal_goals')->where(function ($query) {
            return $query
                ->where('employee_id', 1)
                ->where('goal_type_id', 1)
                ->where('appraisal_identity_id', 1);
        })->ignore($this->appraisal_goal) 
        ],
} 

I successfully deleted using soft delete, but when I created another record and submitted, the validation states that goal_type_id already exists. This is because the record still exists in the Database.

How do I update my validation rules above for unique rules for both create and edit to add where deleted_at is not null?

How

1

1 Answers

0
votes

Just add where('deleted_at', NULL) into the edit rule to ensure you only look for non deleted rows.