Laravel 5.0 blade template and count() conditions

纵然是瞬间 提交于 2019-12-12 12:25:29

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/33937480/laravel-5-0-blade-template-and-count-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!