I'm not sure what I'm doing wrong, but I have a custom form request validation that I'm using in for Create and Update record with unique column validation. It's working fine for creating new record, but not for updating a record.
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!');
}
Getting error when I submit the update form with PUT method It's complaining about the $this
I have this inside the custom form validation for service_name.
Error
Using $this when not in object context
http://localhost:8021/service_type/58
source https://stackoverflow.com/questions/70419652/custom-form-request-validation-with-unique-validation-not-working-on-update
Comments
Post a Comment