【问题标题】:Missing required parameter for [Route: inventories.update][Route:inventories.update] 缺少必需的参数
【发布时间】:2022-01-05 15:24:45
【问题描述】:

我很困惑我需要在哪里解决问题。我收到此错误: [Route: inventory.update] [URI: inventory/{inventory}] [Missing parameter: inventory] ​​缺少必需的参数。 (查看:/var/www/html/resources/views/components/inventory-form.blade.php)

看看,我不知道从哪里开始。 这是我要更新的路由器:

Route::patch('/inventories/{inventory}',[\App\Http\Controllers\InventoryController::class, 'update'])->name('inventories.update');

我的控制器中的编辑和更新功能:

 public function edit(Inventory $inventory): \Illuminate\Contracts\View\View
    {
        return view('pages.inventories.edit',[
            "inventory" => $inventory
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Inventory $inventory
     * @return RedirectResponse
     * @throws ValidationException
     */
    public function update(Request $request, Inventory $inventory): RedirectResponse
    {
        $validated = $this->validate($request, [
            'title'=> 'required|string',
            'description'=> 'required|string|max:300',
            'price' => 'required|integer|min:0',
            'in_stock' => 'required|integer',
            'on_sale' => 'required|boolean'
        ]);
        $inventory->fill($validated)->save();
        return redirect()->route('inventories.index')->with('status',
            'Item has been updated!' . $inventory->title);
    }

还有我的库存形式刀片类:

<?php

namespace App\View\Components;

use App\Models\Inventory;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;

class InventoryForm extends Component
{
    /**
     * @var Inventory|null
     */
    public $inventory;

    /**
     * @param Inventory|null $inventory
     */
    public function __construct(Inventory $inventory = null)
    {
        $this->inventory = $inventory;
    }

    /**
     * @return string
     */
    public function action(): string
    {
        if ($this->inventory) {
            return route('inventories.update', ['inventory' => $this->inventory]);
        }

        return route('inventories.store');
    }

    /**
     * Get the view / contents that represent the component.
     *
     *
     * @return View|\Closure|string
     */
    public function render()
    {
        return view('components.inventory-form');
    }
}

这是我的库存表单刀片文件:

<form action="{{ $action }}" method="post">
    @if($inventory)
        @method('patch')
    @endif
    @csrf
    <label for="title">Enter an item name:</label>
    <input type="text" name="title" value="{{ $inventory?->title }}" required /><br><br>

    <label for="description">Enter the item's description:</label>
    <textarea name="description" required>{{ $inventory?->description }}</textarea><br><br>

    <label for="price">Enter the item's price:</label>
    <input type="number" name="price" value="{{ $inventory?->price }}" required/><br><br>

    <label for="in_stock">Enter a number of items in stock:</label>
    <input type="number" name="in_stock" value="{{ $inventory?->in_stock }}" required/><br><br>

    <label for="on_sale">Select yes/no if item is on sale:</label>
    <select name="on_sale" value="{{ (int) $inventory?->on_sale }}" required>
        <option value="" disabled {{ $inventory ? '' : 'selected' }}>--Please choose an option--</option>
        <option value="1" {{ $inventory?->on_sale ? 'selected' : '' }}>Yes</option>
        <option value="0" {{ (false === $inventory?->on_sale) ? 'selected' : '' }}>No</option>
    </select><br><br>
    <button type="submit">Submit</button>
</form>

目标是显示输入框,以便用户可以将项目输入到主页上的库存中。任何帮助都会很重要!谢谢!

【问题讨论】:

  • 从inventory-form.blade.php 引发的错误,所以发布该代码
  • 您还没有发布您的inventory-form.blade.php 文件,但是您在其中引用了路线而没有提供所需的参数。
  • 那么这个组件从哪里得到它的构造函数的参数呢? ...您调用此组件的代码在哪里?使用该组件的刀片视图
  • 我贴在底部的组件类和库存刀片表单文件。
  • 实际使用该组件的视图在哪里?只是想仔细检查一下设置了哪些属性

标签: php laravel routes


【解决方案1】:

错误不在我的 InventoryForm 类或我的 Inventory-Form.blade.php 中,它在我的 create.blade.php 中,我需要添加 :inventory="null" 以将我的组件呈现到页面。 所以完整的答案是:

<x-inventory-form :inventory="null" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-05
    • 2018-10-28
    • 2018-12-01
    • 2016-06-29
    • 2018-06-20
    • 2020-09-27
    • 1970-01-01
    相关资源
    最近更新 更多