如何在Laravel中使用PUT http动词提交表单 [英] How to submit a form using PUT http verb in Laravel

查看:72
本文介绍了如何在Laravel中使用PUT http动词提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可能已经提出,但我无法解决.如果有人可以帮助我,我将不胜感激.我已经安装了colletive/form,但是答案也可以是html表单标签.

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');

例外:RouteCollection.php第218行中的MethodNotAllowedHttpException:

exception: MethodNotAllowedHttpException in RouteCollection.php line 218:

推荐答案

使用普通的html/blade

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>

Warth Laravel Collective看起来像

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() }}

在两种情况下,都假定您将模型实例 $ casa 传递到刀片模板中

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

在您的控制器中

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());
    }
}

这篇关于如何在Laravel中使用PUT http动词提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆