Still noob and learning Laravel, I am currently in the middle of a simple validation with FormRequest. What I am facing today is the edit of an existing entry. I have written in my FormRequest that I want the name to be unique. I works perfectly but of course when I edit an existing entry, I cannot save it anymore, it already exists... of course it does since I am editing it. Anyway, I found the solution reading the documentation, but unfortunately, it does not work. Here's my code:
Routes:
Route::resource('editeurs', 'PublishersController');
Controller:
class PublishersController extends Controller
{
/* Update an existing publisher */
public function update($slug, PublisherRequest $request)
{
$publisher = Publisher::where('slug', $slug)->firstOrFail();
$input = $request->all();
$publisher->name = $input['name'];
$publisher->slug = my_slug($input['name']);
$publisher->update();
return redirect()->action('PublishersController@show', $publisher->slug);
}
}
FormRequest:
class PublisherRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|unique:publishers,name,'.$this->get('id')
];
}
}
If needed, the view: @section('content') Edition - {!! $publisher->name !!}
{!! Form::model($publisher, ['method' => 'PATCH', 'action' => ['PublishersController@update', $publisher->slug]]) !!}
<div class="large-12 columns">
{!! Form::label('name', 'Nom de l\'éditeur') !!}
{!! Form::text('name', null, ['placeholder' => 'Nom de l\'éditeur']) !!}
</div>
<div class="large-12 columns">
{!! Form::submit('Ajouter un éditeur', ['class' => 'button expand']) !!}
</div>
{!! Form::close() !!}
</div>
@stop
What is wrong with my code?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire