Home > Net >  Undefined variable $inventories
Undefined variable $inventories

Time:12-31

I am trying to go to my edit.blade file on the browser and got this error. I have read through the docs and I am not sure where to go from here. I get the same error for delete as well. My controller:

<?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');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Contracts\View\View
     */
    public function show(int $id)
    {
        $inventories = Inventory::all();
        return view('pages.inventories');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Contracts\View\View
     */
    public function edit(int $id)
    {
        $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'
        ]);
        $inventory = Inventory::find('id');
        $inventory->title = $request->input('title');
        $inventory->description = $request->input('description');
        $inventory->price = $request->input('price');
        $inventory->in_stock = $request->input('in_stock');
        $inventory->on_sale = $request->input('on_sale');
        $inventory->fill($validated)->save();
        return redirect('pages.inventories.edit',['inventory' => $inventory])->with('Item has been updated!' . $request->input('title'));
    }

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

My routes:

<?php

use Illuminate\Support\Facades\Route;

Route::resource('inventory', \App\Http\Controllers\InventoryController::class);

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}',[\App\Http\Controllers\InventoryController::class, 'show'])->name('inventories.show');

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');

My inventories.blade file:

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

@section('content')
    <h1>Inventory Table</h1>
    <p>This is the inventory table made using PHP Laravel that is randomly generated.</p>
   <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Description</th>
                <th>Price</th>
                <th>In stock</th>
                <th>On sale</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>
            @foreach($inventories as $inventory)
            <tr>
                <td>{{$inventory->id}}</td>
                <td>{{$inventory->title}}</td>
                <td>{{$inventory->description}}</td>
                <td> &pound;{{ number_format($inventory->price, 2) }}</td>
                <td>{{$inventory->in_stock}}</td>
                <td>{{ $inventory->on_sale ? 'Yes' : 'No' }}</td>
                <td><a href="{{ route('inventories.show', $inventory) }}">Edit</a></td>
                <td><a href="{{ route('inventories.destroy', $inventory) }}">Delete</a></td>
            </tr>
            @endforeach
        </tbody>
    </table>
@endsection

This is the exact error I get when I click on edit or delete on my inventories page.

ErrorException Undefined variable $inventories (View: /var/www/html/resources/views/pages/inventories.blade.php)

CodePudding user response:

You have to pass invetories to view in show method:

public function show(int $id)
{
    $inventories = Inventory::all();
    return view('pages.inventories', ['inventories' => $inventories]);
}
  1. Your delete button now working as GET request and not DELETE, so if you click on delete button you are redirected to Show again. Delete should be Form with fake DELETE method https://laravel.com/docs/8.x/routing#form-method-spoofing

CodePudding user response:

1. public function show(int $id)  
{  
    $inventories = Inventory::all();  
    return view('pages.inventories', compact('inventories'));  
}

2. Please once check your delete method passing ID.
  • Related