Laravel 5.0 blade template and count() conditions
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
This blog post will discuss how to count specific tasks in a Laravel 5.0 application using the Blade template engine effectively. We'll show you how to handle situations where the data is filtered before rendering, and provide some best practices for clean code and performance optimization.
First Things First: Understanding Collections and Filtering
Before we dive into counting tasks, let's examine collections. Laravel uses collections to manipulate arrays of data in a more efficient way than traditional PHP methods. We can use the 'map', 'each', 'filter', or other collection functions to perform various operations on our data sets. In this case, we want to group and count completed tasks and in-progress tasks from all tasks.Approach 1: Count Completed Tasks with a Custom Collection
To begin, let's create a custom collection that allows us to filter the tasks by their status. We can add this function to our Application class:php
public static function countTasksByStatus($tasks) {
$completed = new Collection();
$inprogress = new Collection();
foreach ($tasks as $task) {
if (Task::$task->completed == 100) {
// completed tasks - we can use the filter() method directly on the collection to return only those with a status of 100% (completed).
$completed = $completed->merge($tasks->where('completed', 100));
} else {
// in progress tasks - we can use the same technique but filter for not completed.
$inprogress = $inprogress->merge($tasks->whereNotIn('completed', [0, 100]));
}
}
return $completed->count() . ' (Completed) / ' . $inprogress->count() . ' (In Progress);
}php
public function index() {
$tasks = Task::All();
return view('pages.projects', compact('tasks'), ['completed' => static::countTasksByStatus($tasks)]);
} php
{{ countTasksByStatus(tasks) }}Approach 2: Count Tasks with Ternary Operator
If you want to make the count shorter, you can use the ternary operator instead of the custom function:php
{{ (count($tasks->where('completed', 100))) . ' (Completed) / (' . (count($tasks->whereNotIn('completed', [0, 100]))) . ') (In Progress); }}Conclusion and Best Practices
Using these techniques, you can easily count and display the desired information from your data set in Laravel applications. However, ensure that you follow best practices, such as: 1. Ensure that your Eloquent models are named appropriately, with singular class names (Task) and plural table names (tasks). 2. Use appropriate collection methods to manipulate and filter your tasks effectively. 3. Create custom helper functions for common computations like counting tasks based on status. 4. Avoid using direct database calls in the view when possible; instead, utilize Eloquent models or QueryBuilder methods. 5. Maintain clean code structure, following Laravel's conventions and best practices. By following these guidelines, you will be able to efficiently and effectively count tasks based on their status in your Laravel application.