user14335805
user14335805

Reputation:

Show counter result in dashboard

I would like to create a dasboard with Laravel 8. I want to count all tickets in the database and display the number in the dashboard. Unfortunately it does not work do you have an idea?

Controller Code


namespace App\Http\Controllers;


use App\Models\Ticket;
use Illuminate\Http\Request;


class DashboardController extends Controller
{

    public function index()
    {
      //
        $ticketsCount = Ticket::count();     
        return view('dashboard.index', compact('ticketsCount'));
    }
} 

View Code

<div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-info">
              <div class="inner">
                <h3>{{ $ticketsCount->count() }}</h3>

                <p>Open Tickets</p>
              </div>
              <div class="icon">
                <i class="ion ion-bag"></i>
              </div>
              <a href="{{ url('tickets') }}" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div> 

Upvotes: 0

Views: 690

Answers (3)

Miqayel Srapionyan
Miqayel Srapionyan

Reputation: 587

As I can see, your router returns view file, and not getting into controller.

Route::get('/dashboard', function () {
    return view('dashboard.index');
});

change your router to (Laravel version before 8)

Route::get('/dashboard', 'DashboardController@index');

After Laravel version 8

use App\Http\Controllers\DashboardController;

Route::get('/dashboard', [DashboardController::class, 'index']);

Docs

Upvotes: 1

user14335805
user14335805

Reputation:

I have tried to get the number of users displayed on the view. Unfortunately I always get an error message...

Error:

ErrorException
Undefined variable: counter (View: /laravel/resources/views/dashboard/index.blade.php)

Route (web):

Route::get('/dashboard', function () {
    return view('dashboard.index');
});

Model (Dashboard)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Dashboard extends Model
{
    use HasFactory;

}

Controller (DashboardController)

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{

    public function index()
    {
      $counter = DB::table('users')->count();
      return view('dashboard.index', compact('counter'));
    }
}

View (dashboard/index.blade.php):

Users: {{ $counter }}

Upvotes: 0

Miqayel Srapionyan
Miqayel Srapionyan

Reputation: 587

Remove ->count() from your view, you already counted it in controller $ticketsCount = Ticket::count();

The view code:

<div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-info">
              <div class="inner">
                <h3>{{ $ticketsCount }}</h3>

                <p>Open Tickets</p>
              </div>
              <div class="icon">
                <i class="ion ion-bag"></i>
              </div>
              <a href="{{ url('tickets') }}" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div> 

Upvotes: 1

Related Questions