How to use update Resource Controllers laravel 4?

2019-05-07 21:08发布

问题:

I have Customer Controller with index, edit, update methods

Route::resource('customer', 'CustomerController');

Controller methods update

public function update($id) { echo $id; }

my HTML Form

<form action="/customer/1" method="post">
<input type="text" name="email" value="" />
<input type="submit" value="" />
</form>

I have following a Documentation here http://four.laravel.com/docs/controllers#resource-controllers PUT/PATCH /resource/{id} update

It's seems not working for me, how to use it? thank you

回答1:

To use the PATH, PUT or DELETE HTML methods you need to add a hidden input with _method. Like the following...

<input type="hidden" name="_method" value="PUT" />


回答2:

You can use the Form Builder. Example using blade:

{{ Form::open(array('method' => 'DELETE')) }}

That will automatically add this for you

<input name="_method" type="hidden" value="DELETE">


回答3:

This works for me in Laravel 4:

{{ Form::open(array('url' => URL::to('customer/1'), 'method' => 'PUT')) }}


回答4:

I am using Laravel resource controller. For update a page, I copied it from insert page after then

Just I added an extra field to update view like as

            {{ method_field('put') }}

Just use this as for update

 <form method="post" action="{{ URL::to('customer',$customer['id'])}}">
            {{ csrf_field() }}
            {{ method_field('put') }}