samedi 24 février 2018

How to pass the entity id to the unique validation in a Form Request on Laravel 5.5?

I have the following route defined:

Route::put('/{organisationId}', 'OrganisationController@update');

And I have the following FormRequest for the update request:

<?php

namespace App\Http\Requests\Organisation;

use Illuminate\Foundation\Http\FormRequest;

class UpdateOrganisationRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'required|min:3|max:60|unique:organisations,name,' . $this->get('organisationId')
        ];
    }
}

And I am trying to use it like this in my controller:

public function update($organisationId, UpdateOrganisationRequest $request)
{
    $organisation = $this->organisationRepository->byId($organisationId);

    if (!$organisation) {
        return $this->error('Organisation not found.', $this::Bad_Request);
    }

    $this->organisationRepository->update($organisation, $request->validated());

    return $this->success(
        fractal($organisation, new OrganisationTransformer())
    );
}

This appears to trigger the unique validation error, because it doesn't appear to exclude the id I am trying to update.

Any ideas why this isn't working?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2sUa6Ky
via IFTTT

Aucun commentaire:

Enregistrer un commentaire