Home > database >  Separation the table and each name follows the table
Separation the table and each name follows the table

Time:01-28

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

<div >
  <div >
    <div >
      <div >
        <div >
          <div >
            <div >
              <div >
                @foreach($packages as $singlePackage)
                  <br>
                  <table >
                    <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 >
                        <td >{{ $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

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Package extends Model
{
    protected $guarded = ['id'];
       
    public function groups()
    {
        return $this->belongsTo(Group::class);
    }
}

PackageController.php

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

CodePudding user response:

Option 1: You can add an ORDER BY group_id in the query

$packages = Package::where('status', 1)->with('groups')->orderBy('group_id')->paginate(getPaginate());

Option 2: You can sort by the group name in the foreach

@foreach($packages->sortBy(function ($item) { return $item->group->name; }) as $singlePackage)

or

@foreach($packages->sortBy(fn($item) => $item->group->name) as $singlePackage)

Option 3: Use the Collection's groupBy method.

<div >
  @foreach($packages->groupBy(function ($item) { return $item->groups->name;  }) as $name => $group)
    <br>
    <table >
      <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 >
            <td >{{ $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>
  •  Tags:  
  • Related