这可能很简单,但我不知道如何在Laravel的视图中声明和递增整数变量.

我使用了几个foreach循环:

@foreach($fans as $fan)
    @foreach ($array as $x)
         @if($fan->fbid==$x)

         @endif
    @endforeach
  @endforeach

I would like to throw in an integer variable $a, that counts the number of times it makes it through the if statement. Like:

$a=0;
@foreach($fans as $fan)
        @foreach ($array as $x)
             @if($fan->fbid==$x)
             $a++;                  
             @endif
        @endforeach
  @endforeach

{{$a}}

What is the proper syntax for doing this in a view in Laravel? Thank you.

推荐答案

The Blade {{ }} will echo out what you are doing.

You should do it like this:

<?php $a = 0; ?>
@foreach($fans as $fan)
        @foreach ($array as $x)
             @if ($fan->fbid == $x)
                 <?php $a++; ?>                  
             @endif
        @endforeach
@endforeach

{{$a}}

Laravel相关问答推荐

为什么我的 Laravel 开发服务器不提供 public 文件夹下的文件?

Laravel 语法 GroupBy 获取列到数组

Laravel 5.2 无法打开 laravel.log

为什么 laravel 模型会重复一组数据以及如何(如果可能)只有一组数据?

Laravel 6 Passport 在错误的凭证上返回 400 Bad request

Laravel + SQLite = SQLSTATE[HY000]一般错误:8次try 写入只读数据库

允许的内存大小 134217728 字节用尽(试图分配 20480 字节) Laravel

使用 Laravel 和 Passport 验证失败时响应状态码 401?

Laravel 5.x 中 onUpdate / onDelete 的可用操作

使用'with'时,Laravel belongsTo 返回 null

使用限制排队 Guzzle 请求

调用未定义的函数 mb_strimwidth

在 Eloquent 中按自定义顺序对集合进行排序

为什么在 Laravel 的 DB::select 中使用 DB::raw?

Laravel - DecryptException:'MAC 无效'

Laravel Eloquent模型如何从关系表中获取数据

如何使用 Laravel 迁移在 Mysql 列中添加注释

将参数传递给过滤器 - Laravel 4

如何在 laravel eloquent 中保存布尔值

如何访问 Laravel 集合中的第 n 个项目?