Laravel 5.0刀片模板和count()条件 [英] Laravel 5.0 blade template and count() conditions

查看:184
本文介绍了Laravel 5.0刀片模板和count()条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合(任务),我想算入可能仅刀片模板已完成的任务的内部.

I've a collection (tasks) and I want to count inside may blade template only completed tasks.

例如 任务总数= 7

Ex. Total tasks = 7

已完成的任务总数= 2

Total completed tasks = 2

正在进行的任务总数= 5

Total in progress tasks = 5

我尝试

{{ count($tasks->completed = 100) }}

它有效,但是如果我尝试

And it works, but if I try

{{ count($tasks->completed < 100) }}

我遇到一个错误.

有帮助吗?

对不起,我是laravel的新手.

Sorry, I'm a newbie on laravel.

(laravel 5.0)

(laravel 5.0)

更新

我需要显示以下内容:

已完成(2)/进行中(5).

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'));
}  

在数据库中,任务表有一个已完成"(整数)列,我用它来检查任务的状态(0%到100%,100%已完成).

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

或者这个:

@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'));

在刀片中显示计数:

<p>{{ $completed->count() }} completed</p>
<p>{{ $inprogress->count() }} in progress</p>

这篇关于Laravel 5.0刀片模板和count()条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆