Home > database >  laravel how to nested foreach
laravel how to nested foreach

Time:05-09

so i have data quarter_month and month . each quarter_month has specified data itself. in this case, i want to this in my view :
Quarter_Month_1 :
-Jan
-Feb
-March
Quarter_Month_2 :
-April
-May
-June
and etch..

this is my Controller code

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

this is when i do 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"
      ]

and this is how my view that i want :

<div >
            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>
        

so how do i render on blade like that?

CodePudding user response:

Laravel collection has a method that helps you group the collection. I think you can do this here.

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

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

This should yield something similar to:

[
  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"
     ]
  ]
]

where 4 and 3 indicates the "triwulan" id/number. With this I think it'd be easier for you to manipulate the views.

and in your view:

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