lundi 20 décembre 2021

Custom Form Request Validation with unique validation not working on update

I'm not sure what I'm doing wrong, but I have a custm form request validation that I'm using in for Create and Update record with unique column validation. It working fine for create creating new record, but not in updating.

Custome Form Request

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ServiceTypeRequest extends FormRequest
{

public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'service_name'        => ['required', Rule::unique('service_type', 'Service')->ignore($this->service_type) ],
        'type'                => ['required', 'string'],
        'view_availability'   => ['required', 'boolean'],

    ];
 }
}

Controller Update

public function update(ServiceTypeRequest $request, ServiceType $serviceType)
{
    $validated = $request->validated();

    $service_type = ServiceType::update([
        'Service'               => $validated['service_name'],
        'type'                  => $validated['type'],
        'view_availability'     => $validated['view_availability'],
    ]);

    return redirect()
            ->route('service_type.index')
            ->with('status', 'Service type updated!');
}

Error Getting when I submit the update form with PUT method It's complain about the $this I have inside the custom form validation for service_name.

Error
Using $this when not in object context
http://localhost:8021/service_type/58 


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3e6vtgr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire