laravel 如何嵌套foreach

laravel how to nested foreach

所以我有数据 quarter_month 和 month 。每个 quarter_month 本身都有指定的数据。 在这种情况下,我想这样做:
Quarter_Month_1 :
-简
-二月
-三月
Quarter_Month_2 :
-四月
-可能
-六月
和蚀刻..

这是我的控制器代码

public function index()
    {
        $dth = DTH::all();
        return view('Triwulan.index',compact('dth'));
    }

这是我做的时候 dd($dth) :

#items: array:7 [▼
    0 => App\Models\DTH {#363 ▼
      #table: "dth"
      #primaryKey: "id"
      #fillable: array:12 [▶]
      #connection: "mysql"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #escapeWhenCastingToString: false
      #attributes: array:13 [▼
        "id" => 1
        "kode_akun" => "bagus"
        "jenis_pajak" => "bagus"
        "nominal_pajak" => "50000"
        "npwp" => "Value"
        "nama_wp" => "Bagus"
        "ntpn" => "1234"
        "id_billing" => "123124"
        "keperluan" => "asdas"
        "bulan" => "Oktober"
        "triwulan" => "4"
        "created_at" => "2022-04-28 19:26:42"
        "updated_at" => "2022-04-28 19:26:42"
      ]
      #original: array:13 [▼
        "id" => 1
        "kode_akun" => "bagus"
        "jenis_pajak" => "bagus"
        "nominal_pajak" => "50000"
        "npwp" => "Value"
        "nama_wp" => "Bagus"
        "ntpn" => "1234"
        "id_billing" => "123124"
        "keperluan" => "asdas"
        "bulan" => "Oktober"
        "triwulan" => "4"
        "created_at" => "2022-04-28 19:26:42"
        "updated_at" => "2022-04-28 19:26:42"
      ]

这就是我想要的观点:

<div class="card mb-4">
            Quarter 1
            <ul>
                <li>Januari</li>
                <li>Februari</li>
                <li>March</li>
            </ul>
            Quarter 2
            <ul>
                <li>April</li>
                <li>May</li>
                <li>June</li>
            </ul>
        

那么我如何在 blade 上渲染?

Laravel 集合有一个方法可以帮助您对集合进行分组。我想你可以在这里做。

$groupedQuarterly = $dth->mapToGroups(function ($item, $key) {
    return [$item['triwulan'] => $item];
})->toArray();

return view('Triwulan.index',compact('groupedQuarterly'));

这应该产生类似于:

[
  4 => [
    0 => [
      "id" => 2
      "kode_akun" => "bagus"
      "jenis_pajak" => "bagus"
      "nominal_pajak" => "50000"
      "npwp" => "Value"
      "nama_wp" => "Bagus"
      "ntpn" => "1234"
      "id_billing" => "123124"
      "keperluan" => "asdas"
      "bulan" => "Oktober"
      "triwulan" => "4"
      "created_at" => "2022-04-28 19:26:42"
      "updated_at" => "2022-04-28 19:26:42"
    ]
    1 => [
      "id" => 1
      ...
      "bulan" => "Nov"
      "triwulan" => "4"
      "created_at" => "2022-04-28 19:26:42"
      "updated_at" => "2022-04-28 19:26:42"
    ]
  ]
  3 => [
    0 => [
          ...
          "triwulan" => "3"
     ]
  ]
]

其中 4 和 3 表示“triwulan”id/number。有了这个,我认为您可以更轻松地操作视图。

在您看来:

@foreach( $$groupedQuarterly as $i => $quarter )
<div>
  Quarter {{ $i }}
  @foreach( $qurater as $month )
   <div>{{ $month["bulan"] }}</div>
  @endforeach
</div>
@endforeach