I want Make Different on Table, Each table has a service name Dontt Need Loop Again Table same Group Name, Each group has the name of the service
Check : https://i.ibb.co/NTnynGq/639.png
View : Package.blade.php
JavaScript
x
<div class="pt-120 pb-120">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="table-responsive--md">
<div class="services-table w-100">
<div class="service-group">
<div class="card">
@foreach($packages as $singlePackage)
<br>
<table class="table table-bordered style--two">
<thead>
<tr>
<th style="width:70%">
<span>Group Name : {{ $singlePackage->groups->name }}</span>
</th>
<th style="width:15%">
<span>@lang('Services Name')</span>
</th>
<th style="width:15%">
<span>@lang('Services Name')</span>
</th>
<th style="width:15%">
<span>@lang('Action')</span>
</th>
</tr>
</thead>
<tbody>
<tr class="block item">
<td class="word-break">{{ $singlePackage->name }}</td>
<td>
<span>{{ $singlePackage->delivery_time }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
</tr>
</tbody>
</table>
@endforeach
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Package Model
JavaScript
<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Package extends Model
{
protected $guarded = ['id'];
public function groups()
{
return $this->belongsTo(Group::class);
}
}
PackageController.php
JavaScript
public function packages()
{
$pageTitle = 'Packages';
$packages = Package::where('status', 1)->with('groups')->paginate(getPaginate());
return view('package',compact('pageTitle', 'packages'));
}
Want Separation each group has the name of the service
Advertisement
Answer
Option 1: You can add an ORDER BY group_id
in the query
JavaScript
$packages = Package::where('status', 1)->with('groups')->orderBy('group_id')->paginate(getPaginate());
Option 2: You can sort by the group name in the foreach
JavaScript
@foreach($packages->sortBy(function ($item) { return $item->group->name; }) as $singlePackage)
or
JavaScript
@foreach($packages->sortBy(fn($item) => $item->group->name) as $singlePackage)
Option 3: Use the Collection
‘s groupBy
method.
JavaScript
<div class="card">
@foreach($packages->groupBy(function ($item) { return $item->groups->name; }) as $name => $group)
<br>
<table class="table table-bordered style--two">
<thead>
<tr>
<th style="width:70%">
<span>Group Name : {{ $name }}</span>
</th>
<th style="width:15%">
<span>@lang('Services Name')</span>
</th>
<th style="width:15%">
<span>@lang('Services Name')</span>
</th>
<th style="width:15%">
<span>@lang('Action')</span>
</th>
</tr>
</thead>
<tbody>
@foreach($group as $singlePackage)
<tr class="block item">
<td class="word-break">{{ $singlePackage->name }}</td>
<td>
<span>{{ $singlePackage->delivery_time }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
<td>
<span>{{ $singlePackage->price }}</span>
</td>
</tr>
@endforeach
</tbody>
</table>
@endforeach
</div>