简体   繁体   中英

Laravel - Request Rules Validation for update not working

In my Laravel5.8 project I have this Unique Rules validation code for create:

MYSQL

ALTER TABLE `hr_holiday_dates`
  ADD UNIQUE KEY `holidaydates_unique_key` ('group_id','holiday_name','date_from');

Model

class HrHolidayDate extends Model
{
  protected $table = 'hr_holiday_dates';
  protected $primaryKey = 'id';

  protected $fillable = [
              'holiday_name',
              'group_id',
              'date_from',
              'date_to',
              'description',
          ];

  public function holidaygroup()
  {
    return $this->belongsTo('App\Models\Hr\HrHolidayGroup','group_id');
  }

}

Controller

public function edit($id)
{
    $userCompany = Auth::user()->company_id;

    abort_unless(\Gate::allows('holiday_date_edit'), 403);
    $holiday = HrHolidayDate::where('id', $id)->first();   
    $holidaygroups = HrHolidayGroup::where('company_id', $userCompany)->get();
    return view('hr.holiday_dates.edit')->with('holiday', $holiday)->with('holidaygroups', $holidaygroups);
}

public function update(UpdateHolidayDateRequest $request, $id)
{
    $dateFrom = Carbon::parse($request->date_from);
    $dateTo = Carbon::parse($request->date_to);

    $holiday = HrHolidayDate::find($id);                            
    $holiday->holiday_name = $request->holiday_name;
    $holiday->group_id = $request->group_id;
    $holiday->date_from = $dateFrom;
    $holiday->date_to = $dateTo;
    $holiday->description = $request->description;
    $holiday->save();
    Session::flash('success', 'Holiday is updated successfully');
    return redirect()->route('hr.holiday_dates.index');
}

Rules

    'holiday_name' => [
        'required', 
        'min:3',
        'max:80',
            Rule::unique('holiday_dates')->where(function ($query) {
            return $query->where('group_id', $this->group_id)
                    ->where('holiday_name', $this->holiday_name)
                    ->where('date_from', Carbon\Carbon::parse($this->date_from)->format('Y-m-d'));
            })               
    ],  

which is working.

holiday_name is the main field and it is unique with regards to group_id and date_from. holiday_dates is the table name while holiday_date is the route.

How I tried to do this for Unique Rules Validation for update, but it is not working:

    'holiday_name' => 
    [
        'required', 
        'min:3',
        'max:80',
        Rule::unique('holiday_dates')->where(function ($query) {
        return $query
            ->where('group_id', 1)
            ->where('holiday_name', 1)
            ->where('date_from', Carbon\Carbon::parse($this->date_from)->format('Y-m-d'));
    })->ignore($this->holiday_date) 
    ],                            

How do I resolve it.

Thank you

Alright. You have id as the primary key. So when you are going to update the resource then you have the model instance as something follows.

$hrHolidayDate = HrHolidayDate::find($id); // or any other way to get the model instance.

and update the rules as follow.

Rules

'holiday_name' => 
    [
        'required', 
        'min:3',
        'max:80',
        Rule::unique('holiday_dates')->where(function ($query) {
        return $query
            ->where('group_id', 1)
            ->where('holiday_name', 1)
            ->where('date_from', Carbon\Carbon::parse($this->date_from)->format('Y-m-d'));
    })->ignore($hrHolidayDate->id)
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM