-5

I'm setting up a loop whenever i tried to print table first row print good with category name and then next row print table with ONE tr(row) data and closed the tag and again start new row and print next data with one row...

i want to print all data with respect to its category name without break multiple table

@if( ! empty($packages) )
    @php
        $serviceId=null;
    @endphp
    @foreach($packages as $package)
        @if(is_null($serviceId))
        <!--CATEGORY NAME PRINT-->
        <div class="row" id="">
          <div class="col-sm-12">
            <div class="well wellHeader">
              <h2>{{ $package->service->name }}</h2>
            </div>
          </div>
        </div>
        <!--CATEGORY NAME PRINT-->
        @php
            $serviceId=$package->service->id
        @endphp

        @endif

        @if($serviceId != $package->service_id)
        <!--CATEGORY NAME PRINT-->
        <div class="row" id="">
          <div class="col-sm-12">
            <div class="well wellHeader">
              <h2>{{ $package->service->name }}</h2>
            </div>
          </div>
        </div>
        <!--CATEGORY NAME PRINT-->
        @php
            $serviceId = $package->service_id
        @endphp
        @endif

    <!--SERVICE NAME TABLE-->
    <div class="row">
        <div class="col-sm-12">
            <div class="well">
                <div class="table-responsive">
                    <table class="table table-striped table-sm ">
                        <thead>
                            <tr>
                              <th>@lang('general.package_id')</th>
                              <th>@lang('general.name')</th>
                              <th>@lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
                              <th>@lang('general.minimum_quantity')</th>
                              <th>@lang('general.maximum_quantity')</th>
                              <th>@lang('general.description')</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                              <td>{{ $package->id }}</td>
                              <td>{{ $package->name }}</td>
                              <td>@php $price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
@endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
                              <td>{{ $package->minimum_quantity }}</td>
                              <td>{{ $package->maximum_quantity }}</td>
                              <td>{{ $package->description }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>

    </div>
    !--SERVICE NAME TABLE-->
    @endforeach

    @else
    No Record Found
    @endif

enter image description here

Epyc Dev
  • 282
  • 2
  • 9
  • 3
    Wait, this isn't about JavaScript...why did you tag it when it's not relevant? – VLAZ Jun 20 '19 at 14:37
  • my table is break into multiple check screenshot – Epyc Dev Jun 20 '19 at 14:38
  • 2
    See [`break`](https://www.php.net/manual/en/control-structures.break.php) and possibly [`continue`](https://www.php.net/manual/en/control-structures.continue.php) – p.s.w.g Jun 20 '19 at 14:39
  • 1
    See https://stackoverflow.com/questions/45189524/how-to-break-a-foreach-loop-in-laravel-blade-view – FunkyMonk91 Jun 20 '19 at 14:42

1 Answers1

2

You can create an extra array to map this,

In method

$packagesNew = [];
foreach($packages as $k => $v)
{
  $packagesNew[$v->service->name]['name'] = $v->service->name;
  $packagesNew[$v->service->name]['id'] = $v->service->id;
  $packagesNew[$v->service->name]['service_id'] = $v->service_id;
  $packagesNew[$v->service->name]['data'][] = $v;
}

pass packagesNew to view

Then I made changes in view, please check.

@if( ! empty($packagesNew) )
@php
$serviceId=null;
@endphp
@foreach($packagesNew as $k => $package)
@if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
    <div class="col-sm-12">
        <div class="well wellHeader">
            <h2>{{ $k }}</h2>
        </div>
    </div>
</div>
<!--CATEGORY NAME PRINT-->
@php
$serviceId=$package['id'];
@endphp
@endif
@if($serviceId != $package['service_id'])
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
    <div class="col-sm-12">
        <div class="well wellHeader">
            <h2>{{ $k }}</h2>
        </div>
    </div>
</div>
<!--CATEGORY NAME PRINT-->
@php
$serviceId = $package['service_id'];
@endphp
@endif
<!--SERVICE NAME TABLE-->
<div class="row">
    <div class="col-sm-12">
        <div class="well">
            <div class="table-responsive">
                <table class="table table-striped table-sm ">
                    <thead>
                        <tr>
                            <th>@lang('general.package_id')</th>
                            <th>@lang('general.name')</th>
                            <th>@lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
                            <th>@lang('general.minimum_quantity')</th>
                            <th>@lang('general.maximum_quantity')</th>
                            <th>@lang('general.description')</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($package['data'] as $k1 => $v1)
                        <tr>
                            <td>{{ $v1->id }}</td>
                            <td>{{ $v1->name }}</td>
                            <td>@php $price = isset($userPackagePrices[$v1->id]) ? $userPackagePrices[$v1->id] : $v1->price_per_item;
                                @endphp
                                {{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
                            <td>{{ $v1->minimum_quantity }}</td>
                            <td>{{ $v1->maximum_quantity }}</td>
                            <td>{{ $v1->description }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
!--SERVICE NAME TABLE-->
@endforeach
@else
No Record Found
@endif
Iqbal Butt
  • 196
  • 2
  • 15
Rahul
  • 18,271
  • 7
  • 41
  • 60