how to set multiple title as heading only once and depending values under it Laravel

Viewed 114

I have a ecommerce website which shows multiple variants like size, color etc...I have create a products details array as below.

array:1 [▼
    0 => array:12 [▼
        "proId" => 2268
        "name" => "PAMPERS BABY DRY PANTS LARGE -64 PANTS"
        "oprice" => 1099.0
        "ofprice" => 1044.05
        "slug" => "pampers-pants-large-64s-9-14kg"
        "variants" => array:5 [▼
        0 => array:2 [▼
            "title" => "NOS"
            "items" => array:1 [▼
            0 => array:7 [▼
                "items_id" => 567
                "items_value" => "42 NOS"
                "sku" => "1058898"
                "ean" => "4902430645577"
                "mrp" => 699.0
                "mfp" => 664.05
                "combo" => ""
            ]
            ]
        ]
        1 => array:2 [▼
            "title" => "NOS"
            "items" => array:1 [▼
            0 => array:7 [▼
                "items_id" => 568
                "items_value" => "24 pants"
                "sku" => "PM50"
                "ean" => "4902430900485"
                "mrp" => 399.0
                "mfp" => 379.05
                "combo" => ""
            ]
            ]
        ]
        2 => array:2 [▶]
        3 => array:2 [▶]
        4 => array:2 [▼
            "title" => "Weight"
            "items" => array:1 [▼
            0 => array:7 [▼
                "items_id" => 591
                "items_value" => "100gm"
                "sku" => "wet"
                "ean" => "346"
                "mrp" => 436.0
                "mfp" => 33.0
                "combo" => ""
            ]
            ]
        ]
        ]
    ]
]

The code for the above is,

$product = Products::select('products.*', 'brand.name', 'category.id as cat_id')->join('category', 'category.id', '=', 'products.category_id')->join('subcategory', 'subcategory.id', '=', 'products.subcategory_id')->join('brand', 'brand.id', '=', 'products.brand_id', 'left')->where(array('products.slug' => $slug, 'products.status' => 1))->get();

    $datas = [];

    foreach ($product as $pro) {
        $item = [
            "proId" => $pro->id,
            "name" => $pro->title,
            "oprice" => $pro->org_price,
            "ofprice" => $pro->off_price,
            "slug" => $pro->slug,
            "variants" => []
        ];

        $variantlist = Provariants::select('products_variants.id', 'variants.name', 'products_variants.variants_type_id', 'products_variants.sku', 'products_variants.ean', 'products_variants.price', 'products_variants.mfp', 'products_variants.combo')->join('variants_type', 'variants_type.id', '=', 'products_variants.variants_type_id')->join('variants', 'variants.id', '=', 'variants_type.variants_id')->where(array('products_variants.products_id' => $pro->id, 'products_variants.status' => 1))->get();

        foreach ($variantlist as $list) {
            $typelist = Variantstype::where(array('id' => $list->variants_type_id, 'status' => 1))->get();

            $subitems = [];

            foreach ($typelist as $type) {
                $subitems[] = [
                    "items_id" => $list->id,
                    "items_value" => $type->name,
                    "sku" => $list->sku,
                    "ean" => $list->ean,
                    "mrp" => $list->price,
                    "mfp" => $list->mfp,
                    "combo" => $list->combo
                ];
            }

            $item['variants'][] = [
                "title" => $list->name,
                "items" => $subitems,
            ];
        }

        $datas[] = $item;
    }

Now the problem is, the title is showing multiple times and one items displaying under it. To get an idea check below image.

https://i.stack.imgur.com/tLjc9.png

I want title to be shown once and all the items under each title should be place together. Please check the required output.

https://i.stack.imgur.com/RFddf.png

Current result code is,

@if (count($pro['variants']) > 0)
            @foreach ($pro['variants'] as $variants)
            <div class="variants mt-4">
                <div>
                    <h6>{{ $variants['title'] }}</h6>
                    @foreach ($variants['items'] as $var_items)
                    <p><a href="{{ URL::current() }}?pid={{ $pro['sku'] }}&attr={{ $var_items['items_id'] }}">{{ $var_items['items_value'] }}</a></p>
                    @endforeach
                </div>
            </div>
            @endforeach
@endif

How can I achieve the required output?

2 Answers

A similar solution to the one by @sta, but I think this might be a little more flexible.

@php
  $title = '';
@endphp
@foreach ($pro['variants'] as $variant)
  <div class="variants mt-4">
    <div>
      @if($title !== $variant['title'])
        <h6>{{ $variant['title'] }}</h6>
      @endif
      @foreach($variant['items'] as $var_item)
        <p><a href="{{ URL::current() }}?pid={{ $pro['sku'] }}&attr={{ $var_item['items_id'] }}">{{ $var_item['items_value'] }}</a></p>
      @endforeach
    </div>
  </div>
  @php
    $title = $variant['title'];
  @endphp
@endforeach

I have not tested this, but it should write out the title every time $variant['title'] changes.

This will work :

@php
   $x = 1;
@endphp
@foreach ($pro['variants'] as $variants)
   <div class="variants mt-4">
      <div>
        @if($x == 1)
          <h6>{{ $variants['title'] }}</h6>
        @endif
        @php
          $x += $x;
        @endphp
        @foreach ($variants['items'] as $var_items)
          <p><a href="{{ URL::current() }}?pid={{ $pro['sku'] }}&attr={{ $var_items['items_id'] }}">{{ $var_items['items_value'] }}</a></p>
        @endforeach
      </div>
   </div>
@endforeach

Above code tested here

Related