I am trying to achieve the following with Eloquent :
I want to query my DB, count the amount of rows where status = 'waiting' and 'inprogress', but I'm running into the following problem. If I run the get() and then try to count, I get told I can't on a non-object. If I try to run the get() after, I get this error : Undefined property: Laravel\Database\Query::$source.
Here are my two attempts :
//get() before
$devs = Dev::todo($user_id)->get(array('id', 'type', 'title', 'source', 'priority', 'status', 'for_user', 'priority', 'desc', 'created_at'));
$devs->num_waiting = $devs->where('status', '=', 'waiting')->count();
$devs->num_inprogress = $devs->where('status', '=', 'inprogress')->count();
//get() after
$devs = Dev::todo($user_id);
$devs->num_waiting = $devs->where('status', '=', 'waiting')->count();
$devs->num_inprogress = $devs->where('status', '=', 'inprogress')->count();
$devs->get(array('id', 'type', 'title', 'source', 'priority', 'status', 'for_user', 'priority', 'desc', 'created_at'));
My todo function :
public static function todo($user_id) {
$todo = Dev::where('for_user', '=', $user_id)
->where(function($query) {
$query->where('status', '=', 'active')
->or_where('status', '=', 'inprogress')
->or_where('status', '=', 'waiting');
})
->order_by('priority', 'asc')
->order_by('created_at', 'desc');
return $todo;
}
How can I run get() after counting the data I need to count, why is this happening, and is there a better way to do this?
Without looking at the Eloquent source code, I'd guess that count() is based on the SQL aggregate function COUNT(). Aggregate functions return, well, aggregate results. They don't return the rows that make up the aggregate.
I'd expect this line (from your code) to give you the count.
If I needed the rows that make up the count, I'd do one of these things, depending on the application.