在包含的集合中使用foreach循环

时间:2018-03-14 10:34:05

标签: laravel-5 collections

我希望只在包含' transCode'的集合中使用foreach获取记录。或者#transcode'存在。该集合包含数组中的其他项,但我只想在包含

的地方进行预处理

这是我的收藏品

Collection {#519 ▼ 

#items: array:19 [▼
    754924 => Collection {#500 ▼
      #items: array:4 [▼
        0 => {#441 ▼
          +"Staff_Number": "754924"
          +"opening_bal": "426.44"
        }
        1 => {#465 ▼
          +"Staff_Number": "754924"
          +"total": "390.00"
          +"transCode": "CONTRIBUTION"
        }
        2 => {#478 ▼
          +"Staff_Number": "754924"
          +"total": "136.44"
          +"transCode": "INTEREST"
        }
        3 => {#497 ▼
          +"Staff_Number": "754924"
          +"total": "-100.00"
          +"transCode": "PARTIAL WITHDRAWAL"
        }
      ]
    }
    754928 => Collection {#501 ▼
      #items: array:3 [▼
        0 => {#436 ▼
          +"Staff_Number": "754928"
          +"opening_bal": "311.81"
        }
        1 => {#464 ▼
          +"Staff_Number": "754928"
          +"total": "231.00"
          +"transCode": "CONTRIBUTION"
        }
        2 => {#479 ▼
          +"Staff_Number": "754928"
          +"total": "80.81"
          +"transCode": "INTEREST"
        }
      ]
    }
    754981 => Collection {#502 ▶}
    754989 => Collection {#503 ▶}
    754995 => Collection {#504 ▶}

所以我会得到像

这样的东西
CONTRIBUTION: 390.00 
INTEREST: 136.44 
PARTIAL WITHDRAWAL: -100.00

这样的事情

@foreach ($results->where contains as $row)

  {{ $row->transCode }}: {{ $row->total }}

@endforeach 

任何帮助?

1 个答案:

答案 0 :(得分:0)

您正在寻找的是对您的收藏品进行过滤。

这应该会给你一个启示:

https://laravel.com/docs/5.6/collections#method-filter

在控制器内部尝试这样做,以便你的视野保持清洁