Laravel 5.0 刀片模板和 count() 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33937480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.0 blade template and count() conditions
提问by natas
I've a collection (tasks) and I want to count inside may blade template only completed tasks.
我有一个集合(任务),我想在可能刀片模板中只计算完成的任务。
Ex. Total tasks = 7
前任。总任务数 = 7
Total completed tasks = 2
完成的任务总数 = 2
Total in progress tasks = 5
进行中的任务总数 = 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 的新手。
(laravel 5.0)
(laravel 5.0)
UPDATE
更新
I need to show something like:
我需要展示类似的东西:
Completed (2) / in progress (5).
已完成 (2) / 进行中 (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).
在数据库中,任务表有一个“已完成”(整数)列,我用它来检查任务的状态(0% 到 100%,100% 已完成)。
回答by Bj?rn
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>