简体   繁体   中英

How to get count number of rows in database and show in laravel 7

I need to get opened jobs count and show in the view. There are 6 opened jobs. i need to show 6 in green box. please see attachment

附件

 <!-- opend job view -->
        <!-- ./col -->
          <div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-success">
              <div class="inner">
                <h3>53<sup style="font-size: 20px">%</sup></h3>
    
                <p>Opened</p>
              </div>
              <div class="icon">
                <i class="fas fa-box-open"></i>
              </div>
              <a href="opened" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div>

 <!-- opend job controler-->
public function open_count ()
    {
        $pend_counts = Job::where('state','Closed')->count();
        // return view('jobs.closed_job',compact('closed'));
        // $count = App\Flight::where('active', 1)->count();
    }

 <!-- Route-->
Route::get('/open_c','JobController@open_count');

You have to get the count in your Controller and pass it to the View (dont forget that):

public function open_count ()
    {
        $opened_counts = Job::where('state','Closed')->count();
    
        //Add the $opened_counts variable to the compact function
        return view('jobs.closed_job',compact('opened_counts'));
       
    }

In your Blade file you can then call {{$opened_counts}} to display the variable.

<div class="col-lg-3 col-6">
            <!-- small box -->
            <div class="small-box bg-success">
              <div class="inner">
                <h3> {{$opened_counts}} <sup style="font-size: 20px">%</sup></h3>
    
                <p>Opened</p>
              </div>
              <div class="icon">
                <i class="fas fa-box-open"></i>
              </div>
              <a href="opened" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
            </div>
          </div>

If you need the percentage and not the absolute value you can change the controller to (you will also need to handle the divison by 0 error):

public function open_count ()
    {
        $opened_counts = Job::where('state','Closed')->count();
        $percentage = $opened_counts / Job::all()->count();
    
        //Add the $opened_counts variable to the compact function
        return view('jobs.closed_job',compact('opened_counts', 'percentage'));
       
    }

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