How to select count with Laravel's fluent query builder? How to select count with Laravel's fluent query builder? laravel laravel

How to select count with Laravel's fluent query builder?


You can use an array in the select() to define more columns and you can use the DB::raw() there with aliasing it to followers. Should look like this:

$query = DB::table('category_issue')    ->select(array('issues.*', DB::raw('COUNT(issue_subscriptions.issue_id) as followers')))    ->where('category_id', '=', 1)    ->join('issues', 'category_issue.issue_id', '=', 'issues.id')    ->left_join('issue_subscriptions', 'issues.id', '=', 'issue_subscriptions.issue_id')    ->group_by('issues.id')    ->order_by('followers', 'desc')    ->get();


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

will give you the number of items.

For more detailed information check Fluent Query Builder section in beautiful Laravel Documentation.