Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.0 blade template and count() conditions

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).

like image 296
natas Avatar asked Nov 26 '15 11:11

natas


1 Answers

I don't think you are using the correct way to do this. You are doing a count and compare in one function.

Comparing in blade

@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

(Update:) Filtering collection and counting in blade:

$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>
like image 68
Björn Avatar answered Oct 04 '22 02:10

Björn