Search code examples
phplaravelcountadminvoyager

Laravel - voyager admin dimmer statistic


I have laravel project with voyager admin panel. Right now I need to see statistics on my admin dashboard for products in stores but only from today. So it needs to show me all products made today, to count it and show it on my dashboard.Let me show my code: Here is my DeltaDimmer.php:

<?php

namespace TCG\Voyager\Widgets;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Facades\Voyager;
use Carbon\Carbon;
use App\Storeone;

class DeltaDimmer extends BaseDimmer
{
    /**
     * The configuration array.
     *
     * @var array
     */
    protected $config = [];

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        $count = Storeone::where('created_at', '=', Carbon::today())->count();
        //$count = \App\Storeone::where('created_at', '=', Carbon::today())->count();
        $string = 'Rad: ';

        return view('voyager::dimmer', array_merge($this->config, [
            'icon'   => 'voyager-eye',
            'title'  => "{$string} {$count}",
            'text'   => 'Optika Podgorica/Delta',
            'button' => [
                'text' => 'Prikazi',
                'link' => route('voyager.optikadelta.index'),
            ],
            'image' => voyager_asset('images/widget-backgrounds/04.jpeg'),
        ]));
    }

    /**
     * Determine if the widget should be displayed.
     *
     * @return bool
     */
    public function shouldBeDisplayed()
    {
        return Auth::user()->can('browse', Voyager::model('Post'));
    }
}

So this is my DeltaDimmer.php. This line $count = Storeone::where('created_at', '=', Carbon::today())->count(); should count my products in Storeone only for today but right now it is showing me 0 and I made a few a products just for test. What I'm doing wrong?


Solution

  • use whereDate instead of comparing with only where

    $count = Storeone::whereDate('created_at', Carbon::today())->count();