Laravel 5.0 blade template and count() conditions

48,683

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>
Share:
48,683
natas
Author by

natas

Updated on July 09, 2022

Comments

  • natas
    natas almost 2 years

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

  • natas
    natas over 8 years
    No, I need to show something like: Completed (2) / in progress (5). I need to count...
  • Björn
    Björn over 8 years
    Can you add some more information in your openings post? Like your task model + relations, and how do you retrieve your tasks in the controller?