ErrorException 未定义变量 $inventory

ErrorException Undefined variable $inventory

当我点击“编辑”来编辑我的清单 table 中的一个条目时,我目前遇到一个错误,提示我没有定义 $inventory。当我似乎在控制器中正确传递它时,我对为什么会这样感到困惑。

这是我的 edit.blade 文件:

@extends('layouts.app')

@section('title', 'Edit Inventory')

@section('menu')

@section('content')
    <h1><strong>Edit Inventory {{ $inventory->title }}</strong></h1>
    {{ $inventory }}
    <x-inventory-form :inventory=$inventory
    @method('patch') />
    <input type="hidden" name="_method" value="patch">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
@endsection

这是我的控制器(具体看编辑):

<?php

namespace App\Http\Controllers;

use App\Models\Inventory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Log;

class InventoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function index()
    {
        $inventories = Inventory::all();
        return view('pages.inventories',[
            "inventories" => $inventories
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function create()
    {
        return view('pages.inventories.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Redirector
     */
    public function store(Request $request)
    {
        $validated = $request->validate([
            'title'=> 'required|string',
            'description'=> 'required|string|max:300',
            'price' => 'required|integer|min:0',
            'in_stock' => 'required|integer',
            'on_sale' => 'required|boolean'
        ]);
        $inventory = new Inventory();

        $inventory->fill($validated)->save();

        return redirect('/inventories');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Contracts\View\View
     */
    public function edit(Inventory $inventory)
    {
        $inventories = Inventory::all();
        return view('pages.inventories.edit',[
            "inventories" => $inventories
        ]);
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return RedirectResponse
     */
    public function destroy(Inventory $inventory)
    {
        $inventory->delete();
        return redirect()->route('/inventories')->with('Item has been deleted!');
    }
}

这是我的路由器:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', [\App\Http\Controllers\HomeController::class, 'index'])->name('pages.index');

Route::get('/inventories', [\App\Http\Controllers\InventoryController::class, 'index'])->name('index');

Route::get('/inventories/create', [\App\Http\Controllers\InventoryController::class, 'create']);

Route::post('/inventories', [\App\Http\Controllers\InventoryController::class, 'store']);

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

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

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

任何帮助都会很好。谢谢!!!

您在 blade 文件中使用了变量“inventory”,但是在您的控制器的编辑功能中,您传递了名为“inventories”的变量。