计算项目表中有多少个项目


Count how many projects are in the projects table

我正试图计算我在项目表中有多少项目。我的代码目前获得特定id的项目计数。

我只想计数表中存在的所有项目。

控制器代码:

public function countProjects()
{
    $count = Projects::where('id','0')->count();
    return view('projects.test')->with('count', $count);
}

你应该可以直接删除where。

$count = Projects::count();

where()构建器方法在附加条件后返回$this,以便允许方法链接。因此,如果您消除它,您将仍然调用模型上的count()

如laravel网站所述

https://laravel.com/docs/5.3/queries

$users = DB::table('users')->count();

同样,你可以这样使用

public function countProjects()
{
    $count = DB::table('Projects')->where('id','0')->count();
    return view('projects.test')->with('count', $count);
}

其他选项是如此链接所示https://laravel.com/docs/5.3/eloquent

$count = App'Flight::where('active', 1)->count();

与您的代码匹配。我建议只需在控制器中打印$count并检查变量中究竟有哪些数据,然后相应地在代码中进行更改。

我想这会有帮助

尝试使用Projects::get()->count()