1

I have create a validation request file on request folder. It working fine in new insert. But when i update, it's not working also i pass the unique $id but not working.

a. Resource controller update method

public function update(KlassNameRequest $request, $id)
{
    $validated = $request->validated();
    KlassName::where('id', $id)->update($validated);
}

b. Validation code

public function rules()
{
    return [
        'name'          => 'required|unique:klass_names|max:128,' . $this->id,
        'ref'           => 'required|unique:klass_names|numeric|between:1,999,' . $this->id,
        'seat_quota'    => 'required|numeric|between:1,9999',
        'division'      => 'required',
        'semester'      => 'required',
    ];
}

This message shows me when i update

This message shows me when i update

mahbub
  • 103
  • 1
  • 3
  • 13

3 Answers3

0

Just try this one

    'name' => ['required','max:128',Rule::unique('klass_names')->where(function ($query) {
    return $query->where('name', $this->name);
})],

'ref' => ['required','max:128',Rule::unique('klass_names')->where(function ($query) {
        return $query->where('ref', $this-> ref);
    })],

Hope it will solve the problem

Wael Khalifa
  • 895
  • 7
  • 17
0

return [ 'name' => 'required|unique:klass_names,' . $this->id.'|max:128', 'ref' => 'required|unique:klass_names,' . $this->id.'|numeric|between:1,999', 'seat_quota' => 'required|numeric|between:1,9999', 'division' => 'required', 'semester' => 'required', ];

0

I have solve my problem in this way -

a. Add extra input hidden field passing id for $request method. Because my route is resource group route -

<form action="{{ route('adm.kls.update', $kls->id) }}" method="post">
    @csrf
    @method('PUT')

    <input type="hidden" name="id" value="{{ $kls->id }}">
</form>

b. Some editing in validation code.

public function rules() {
  return [
    'name' => 'required|max:128|unique:klass_names,name,' . $this->id,
    'ref' => 'required|numeric|between:1,999|unique:klass_names,ref,' . $this->id,
    'seat_quota' => 'required|numeric|between:1,9999',
  ];
}

done.

mahbub
  • 103
  • 1
  • 3
  • 13