【问题标题】:How to submit a form using PUT http verb in Laravel如何在 Laravel 中使用 PUT http 动词提交表单
【发布时间】:2017-07-01 20:02:09
【问题描述】:

我知道这个问题可能已经提出,但我无法让它发挥作用。如果有人可以帮助我,我将不胜感激。我已经安装了 collective/form,但答案也可以是 html 表单标签。

现在列出我的表单、我的路线和我的异常。

{{ 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:

【问题讨论】:

  • 您提出了什么要求?你的路线是casas/1(或其他ID)吗?
  • 你不需要把你做一个简单的post

标签: php laravel rest http


【解决方案1】:

使用纯 html / 刀片

<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 可能看起来像

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

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

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

在你的控制器中

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

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 2018-11-12
    相关资源
    最近更新 更多