Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count number of all rows in the table in Laravel 5.6

I'm new to laravel. I have a table named as projects and, I need to count number of all rows of the projects table. I have tried using id column to count all the rows like the following function

public function totalprojects()
    {
        $projects = Project::where('id')->count();

        return view('summarys.summary')->withProjects($projects);
    } 

but its returning 0 how can I manage this?

like image 772
Muru Avatar asked Nov 20 '25 11:11

Muru


1 Answers

in your controller

(1) using Eloquent :

 use App\Project;

 public function totalprojects()
        {
            $total_projects = Project::count();
            return view('summarys.summary')->with(['total'=>$total_projects]);
        }

(2) using Query Builder :

  use DB;
  public function totalprojects()
            {
                $total_projects = DB::table('projects')->count();
                return view('summarys.summary')->with(['total'=>$total_projects]);
            }

in your blade :

<p>{{ $total }}</p>
like image 55
Saurabh Mistry Avatar answered Nov 22 '25 03:11

Saurabh Mistry