I've a collection (tasks) and I want to count inside may blade template only completed tasks.
Ex. Total tasks = 7
Total completed tasks = 2
Total in progress tasks = 5
I try
{{ count($tasks->completed = 100) }}
And it works, but if I try
{{ count($tasks->completed < 100) }}
I got an error.
Any help?
Sorry, I'm a newbie on laravel.
(laravel 5.0)
UPDATE
I need to show something like:
Completed (2) / in progress (5).
In my controller I've:
public function index()
{
$tasks = Task::All();
$completed = $tasks->where('completed', 100);
//I dunno how...
$inprogress = ...;
return view('pages.projects', compact('projects', 'tasks', 'completed', 'inprogress'));
}
In the db, the tasks table have a 'completed' (integer) column that I use to check the status of the task (0% to 100%, 100% is completed).
I don't think you are using the correct way to do this. You are doing a count and compare in one function.
@if($tasks->completed->count() == 100)
<p>This is shown if its 100</p>
@else
<p>This is shown if its not 100</p>
@endif
Or this one:
@if($tasks->completed->count() < 100)
<p>This is shown if its smaller than 100</p>
@else
<p>This is shown if its 100 or bigger</p>
@endif
$tasks = Task::all();
$completed = $tasks->where('completed', 100);
$inprogress = $tasks->filter(function ($task) {
return $task['completed'] < 100; // Or $task->completed if its an object
});
return view('pages.projects', compact('projects', 'tasks', 'completed', 'inprogress'));
Display the count in your blade:
<p>{{ $completed->count() }} completed</p>
<p>{{ $inprogress->count() }} in progress</p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With