【问题标题】:Laravel Resource default method - updateLaravel 资源默认方法 - 更新
【发布时间】:2018-02-11 20:08:36
【问题描述】:

我有一个名为 ItemController 的控制器。使用此控制器,方法storeindex 是唯一可识别的方法。当我使用方法update时,它会抛出一个错误

缺少 [Route: items.update] 所需的参数

Route.php

            Route::resource('items', 'ItemsController');

ResourceRegistrar.php

protected $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];

html

 <form role="form" method="post"  action="{{ route('items.update') }}">
            {!! csrf_field() !!}

<div class="form-group" style="width: 400px;">
    <label for="exampleInputPassword1">Item Name</label>
     <input type="text" class="form-control" id="exampleInputPassword1" name="name" value="{!! $item->name !!}" placeholder="item name" required="">
</div>


<div class="form-group" style="width: 400px;">
       <label for="exampleInputPassword1">Retail Price</label>
                  <input type="number" step="any"  class="form-control" id="exampleInputPassword1"  value="{!! $item->retail_price !!}"  name="retail_price" placeholder="retail price" required="">
</div>


<div class="form-group" style="width: 400px;">
    <label for="exampleInputPassword1">Quantity Price</label>
                  <input type="number" step="any"  class="form-control" id="exampleInputPassword1"  value="{!! $item->quantity_price !!}" name="quantity_price"
                  placeholder="quantity price " required="">
</div>

     <button type="submit" class="btn btn-primary">Submit</button>


</form>

控制器

   public function update(Request $request, $id)
    {
        $item = Item::findorFail($id);

        $item->name = $request->get('name');
        $item->retail_price = $request->get('retail_price');
        $item->quantity_price = $request->get('quantity_price');

        $item->update($request, $id);

        Session()->flash('flash_message', 'Item successfully updated');
        return redirect()->route('items.index');
    }

这是怎么回事?

【问题讨论】:

  • 请分享更多信息。请求详细信息会很好!

标签: php laravel


【解决方案1】:

当您调用 items.update 路由时,它期望您正在更新的 item,但您没有在请求中传递 item,因此该路由会抛出错误,因为您缺少能够执行的必需项知道你在更新哪一个,这有意义吗?

编辑:

您需要在您的route('items.update', $item-&gt;id) 中包含$item-&gt;id

【讨论】:

  • 你不是,你只是在没有任何传递参数的情况下调用路由
【解决方案2】:

您需要为要更新的项目包含一个路由参数。如果你跑

php artisan route:list

您应该会看到类似:/items/{item} App\Http\Controllers\ItemController

您的请求 URL 应包含项目 ID,例如:

`/items/1`

其中 1 是项目更新的主键。

编辑

您需要将 id 传递给 HTML 表单中的路由辅助方法:

action="{{ route('items.update', $item->id) }}"

另外,在您的表单中,通过以下方式将方法设置为PUTPATCH

<form>
    {!! method_field('put') !!}

    //or 

    {!! method_field('patch') !!}

    ...

</form>

Laravel 使用一种称为Form Method Spoofing 的技术通过POST 伪造PUTPATCHDELETE HTTP 动词。

编辑 2

你保存的记录不正确,改一下

$item->update($request, $id);

$item->save();

【讨论】:

  • 您在路由助手中缺少 id,我已经更新了答案。
  • 我得到一个方法不允许的异常
  • 在表单中包含putpatch方法字段,答案已更新。
  • BadMethodCallException 方法 [显示] 不存在。
  • 嗯,应该是调用更新路由,是重定向到items.index 并且可能失败了吗?
猜你喜欢
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 2021-08-20
  • 2015-11-02
  • 1970-01-01
  • 2016-09-21
  • 2018-08-24
  • 1970-01-01
相关资源
最近更新 更多