php - 如何在 Laravel 中使用 PUT http 动词提交表单

标签 php laravel rest http

我知道这个问题可能已经提出,但我就是无法让它发挥作用。如果有人可以帮助我,我将不胜感激。我安装了集体/表单,但答案也可以是 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:

最佳答案

使用纯 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 它可能看起来像

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

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

在这两种情况下,都假设您将模型实例 $casa 传递到您的 blade 模板中

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

关于php - 如何在 Laravel 中使用 PUT http 动词提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44864942/

相关文章:

java - 删除大于 1000 的 ID 时出现问题

php - Laravel 翻译

php - AngularJS,PHP Restful Cors 问题

PHP用按钮插入MySQL

php - MySQL SELECT 两个值之间的行

php - laravel 队列 - 同步驱动程序如何工作?它是在单独的进程/线程中执行还是在主执行线程中执行?

laravel Blade 包含具有相对路径的文件

javascript - Backbone.js 集合 url 模式

php - 不同表中具有相同名称的字段。获取时如何获取特定的

具有不存在索引的默认值的 PHP 数组