1

I am trying to get a status count of some processes and tasks just like a to-do list tasks count. My current database look like this SQL Database Structure

+----+---------+------------+-------------+---------+------------+---------------------+---------------------+
| id | user_id | list_id    | name        | records | status     | created_at          | updated_at          |
+----+---------+------------+-------------+---------+------------+---------------------+---------------------+
|  1 |       1 | 8Kwvf8ikcV | 10 mails    | 19      | On-Hold    | 2021-03-11 07:56:17 | 2021-03-11 07:56:17 |
|  2 |       1 | a0pJRv4Zfc | temp_emails | 884     | On-Hold    | 2021-03-11 08:02:13 | 2021-03-11 08:02:13 |
|  3 |       1 | xrgZZkrLFA | 10 mails    | 19      | Processing | 2021-03-11 08:06:37 | 2021-03-11 08:06:37 |
|  4 |       1 | lDOX8p2sgU | 10 mails    | 19      | Completed  | 2021-03-11 08:53:51 | 2021-03-11 08:53:51 |
+----+---------+------------+-------------+---------+------------+---------------------+---------------------+

I am using

return $listStats = ListName::select([
            DB::raw("status"),
            DB::raw("COUNT(status) as count")
        ])->where('user_id', Auth::id())
        ->groupBy('status')
        ->get();

Query to get the database and its count.

Current laravel out put is showing as [{"status":"Completed","count":1},{"status":"On-Hold","count":2},{"status":"Processing","count":1}]

My question is how can I get the count of completed status without using foreach loop? Do I need to change my query? or can I simply use $listStats['Completed']['count'] ?

2 Answers 2

1

A foreach will be run at some point unless you use a join in the query to give an order and force a result with all possible status.

One easy solution would be

$StatCount[ //make sure to list all possible statuses here
    "Completed" => 0,
    "Processing" => 0,
    "On-Hold" => 0,
];
foreach ($listStats as $listStat) {
    $StatCount[$listStat->status] = $listStat->count
}

Now you can access your status counts like this $statCount['Completed']

Sign up to request clarification or add additional context in comments.

1 Comment

I used this in my controller Thanks for answering and solving the problem for me. //make sure to list all possible statuses here $statCount = array( "Completed" => 0, "Processing" => 0, "On-Hold" => 0, ); foreach ($listStats as $listStat) { $statCount[$listStat->status] = $listStat->count; }
1

Counting is a pretty lightweight job and it can be done on the fly easily. Instead of creating an extra query to count the records, collect the records and count them in Blade. It's a better idea because you will fetch the tasks anyway and no need to query them twice.

// In controller
$user = User::where('id', Auth::id())->with(['tasks' => function($query) {
  $query->groupBy('status')
}])->get();

return view('viewName')->with(['user', $user]);
// In Blade
$user->tasks->Completed->count();

3 Comments

Hello, Thank you for Answering, Your idea looks great but looks like you miss the table, the table name is list_names and Model name is ListName. Itried your query but there is something wrong I don't know. I am learning laravel and there is lot of different queries to understand me. Can you please rewrite it with proper table name. Also the user_id is already present in the list_names table so cant we use it instead for that. Or Is there any default function to consider user_id automatically. Please help me understand it. :)
If you created eloquent relations between the users and tasks tables correctly, the table names become irrelevant. I used 'tasks' as the relation name, just replace it with the real name like with(['relationsName'] => function...) and $user->relationsName->Completed->count();.
will not work, the groupBy in the relation callback method gonna trigger an exception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.