skip to Main Content

Undefined variable error when I tried to display count in the dashboard page

I try to display count for number of tools in my dashboard page. However, it shows error:
Undefined variable $jumlahalat

web.php:

Route::get('/dashboard-admin', function () {
    $jumlahalat = Inventory::count();
    return view('dashboard-admin', compact('jumlahalat'));
});

dashboard-admin.blade.php:

<div class="grid grid-cols-4 gap-4 rounded mt-4 text-md font-bold text-[#fecbcf] w-full">
    <a href="{{ url('/sda/alat') }}" class="text-white" style="text-decoration: none; color: inherit;">
        <div class="bg-[#5479f7] flex gap-3 pl-5 justify-start items-center rounded-lg h-[110px]">
            <div class="h-[70px] w-[70px] rounded-sm bg-white flex justify-center items-center">
                <span class="material-icons text-black" style="font-size: 40px">inventory_2</span>
            </div>
            <div>
                <div class="self-center text-start">Jumlah Alat</div>
                <div class="self-center text-start">{{ $jumlahalat }}</div>
            </div>
        </div>
    </a>
</div>

I already defined use AppModelsInventory in web.php

2

Answers


  1. This also might be just route/view cache issue.
    Try to run these commands on your project

    php artisan route:clear
    

    and also

    php artisan view:clear
    
    Login or Signup to reply.
  2. counter-image

    if you try to do this counter on your dashboard….
    then you can try on my way…..
    on your controller. you can simply return all of your model….

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use AppModelsProductCategory;
    use AppModelsProducts;
    use AppModelsCustomer;
    
    class PageController extends Controller
    {
        public function dashboard()
        {
            $category = ProductCategory::all();
            $products = Products::all();
            $customer = Customer::all();
            return view('admin/dashboard',compact('category','products','customer'));
        }
    
    }```
    
     and then you can call this on you page for count...
    
                  ```<div class="card-body">
                    <div class=" float-end fs-1 fw-bolder opacity-25">
                    <i class="fa-solid fa-box"></i>
                        </div>
                        <h5 class="card-title text-center">Total Products</h5>
                        <p class="card-text fs-1 text-center fw-bolder">{{$products->count()}}</p>
                    </div>```
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search