I am currently getting an error when I click on edit to edit an entry in my inventory table that says I don't have $inventory defined. I am confused as to why that is, when I seem to have passed it correctly in the controller.
Here is my edit.blade file:
@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
Here is my controller (specifically looking at edit):
<?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!');
}
}
And here is my router:
<?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');
Any help would be nice. Thank you!!!
You use variable "inventory" in the blade file, but at your controller's edit function, you passed variable named "inventories".