How to submit a form using PUT http verb in Larave

2019-07-23 05:16发布

问题:

I know that this question may have been made but I just can't get it to work. if someone could help me I would be very grateful. I have colletive/form installed but the answer can be an html form tag too.

Now listing my form, my route and my exception.

{{ Form::model( array('route' => array('casas.update', 238), 'method' => 'PUT')) }}
  <input type="hidden" name="_method" value="PUT"> 

-

Route::resource('casas', 'CasasController');

exception: MethodNotAllowedHttpException in RouteCollection.php line 218:

回答1:

With plain html / blade

<form action="{{ route('casas.update', $casa->id) }}" method="post">
    {{ csrf_field() }}
    {{ method_field('put') }}

    {{-- Your form fields go here --}}

    <input type="submit" value="Update">
</form>

Wirth Laravel Collective it may look like

{{ Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put']) }}
    {{-- Your form fields go here --}}

    {{ Form::submit('Update') }}
{{ Form::close() }}

In both cases it's assumed that you pass a model instance $casa into your blade template

In your controller

class CasasController extends Controller
{
    public function edit(Casa $casa) // type hint your Model
    {
        return view('casas.edit')
            ->with('casa', $casa);
    }

    public function update(Request $request, Casa $casa) // type hint your Model
    {
        dd($casa, $request->all());
    }
}