简体   繁体   中英

How can I dynamically display Total number of items from the Database in a blade view in Laravel 7

I'm quite new to Laravel, In my Dashboard I would like to display Total number of projects coming from MySQL database table, called "projects" into a Bootstrap card (See the Screenshot) I have some code in my Controller that gets Total number of Projects from the Database but I'm getting "Undefined variable" error. I know my mistake is in my blade view. For now I'm just displaying static data in Bootstrap card. Please Help.

In my HomeController:

public function showTotals()
    {
        $allProjects = Project::count();

        return view('dashboard.index', compact('allProjects'));
    }

In my View:

<div class="col-md-3"
<div class="card-counter primary">
<i class="fa fa-id-card"></i>
<span class="count-numbers">{{ $allProjects }}</span>
<span class="count-name">Projects</span>
</div>
</div>

在此处输入图像描述

在此处输入图像描述

Change to array in return view.Since count return number not an array

  return view('dashboard.index', ['allProjects'=>$allProjects]);

Since view expects second parameter to be an array

 view($view = null, $data = [], $mergeData = [])

It looks like your function showTotals() is not the one called in your route.

I suppose the route is calling in public function index() , where you actually should pass the $allProjects variable.

Either you call this in your index function or you change the routes/web.php to call HomeController@showTotals() . I suggest you should use the first one..

Please post your routes/web.php .

I'm quite new to Laravel, In my Dashboard I would like to display Total number of projects coming from MySQL database table, called "projects" into a Bootstrap card (See the Screenshot) I have some code in my Controller that gets Total number of Projects from the Database but I'm getting "Undefined variable" error. I know my mistake is in my blade view. For now I'm just displaying static data in Bootstrap card. Please Help.

In my HomeController:

public function showTotals()
    {
        $allProjects = Project::count();

        return view('dashboard.index', compact('allProjects'));
    }

In my View:

<div class="col-md-3"
<div class="card-counter primary">
<i class="fa fa-id-card"></i>
<span class="count-numbers">{{ $allProjects }}</span>
<span class="count-name">Projects</span>
</div>
</div>

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM