将同一类别类型的项目分组

发布于 2025-01-21 16:57:24 字数 1487 浏览 0 评论 0原文

我有此方法返回所提供的服务

public function order_services(){
    
    $services = DB::select('select * from services');
    return view('guests/services',['services'=>$services]);;

}

,并且我像这样显示

<ul class="list-group" id="myUl">
   @foreach ($services as $service)
   <li class="list-group-item d-flex justify-content-between align-items-center border border-danger"><span  style="font-size:24px; color:red
      "><i class="bi bi-check-circle font-weight-bold "></i></span> <span style="font-weight:bold" id="service_category">{{ $service->service_category }}</span><span><u><i class="bi bi-magic"></i></u></span></li>
   <li class="list-group-item d-flex justify-content-between align-items-center"><span class="product_name" style="font-size:24px; color:#870FD1" data-price="349.34"><i class="bi bi-plus-circle"></i></span> <span class="product_name" id="service_name" data-price="{{ $service->service_price }}" data-category="{{ $service->service_category }}" >{{ $service->service_name }}</span><span class="text-success"><u>Available</u></span></li>
   @endforeach
</ul>

我的表具有service_name and service_category

我想显示service> servication_category as标题和该类别下的所有服务。这可以在刀片中完成,还是需要首先格式化控制器中的结果?

I have this method that returns the services being offered

public function order_services(){
    
    $services = DB::select('select * from services');
    return view('guests/services',['services'=>$services]);;

}

and i am displaying like this

<ul class="list-group" id="myUl">
   @foreach ($services as $service)
   <li class="list-group-item d-flex justify-content-between align-items-center border border-danger"><span  style="font-size:24px; color:red
      "><i class="bi bi-check-circle font-weight-bold "></i></span> <span style="font-weight:bold" id="service_category">{{ $service->service_category }}</span><span><u><i class="bi bi-magic"></i></u></span></li>
   <li class="list-group-item d-flex justify-content-between align-items-center"><span class="product_name" style="font-size:24px; color:#870FD1" data-price="349.34"><i class="bi bi-plus-circle"></i></span> <span class="product_name" id="service_name" data-price="{{ $service->service_price }}" data-category="{{ $service->service_category }}" >{{ $service->service_name }}</span><span class="text-success"><u>Available</u></span></li>
   @endforeach
</ul>

My table has service_name and service_category

I would like to display service_category as header and all the services under that category. Can this be done within blade or do i need to format the results in my controller first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2025-01-28 16:57:24

您可以将列表与php(在Blade的控制器中为@PHP指令)分组:

$list = [];
foreach($services as $item) {
  $list[$item->service_category][] = $item;
}

更新的方法

public function order_services(){
    

    $services=  DB::table('services')
            ->selectRaw('id,service_name,service_category,service_price')
            ->orderBy('service_category')
            ->get();
            
    $list = [];
    foreach($services as $item) {
      $list[$item->service_category][] = $item;
    }
    
    return view('guests/services',['list'=>$list]);

}

,您可以在Blade中使用$ list以显示结果:

@foreach ($list as $category => $item)
  <h2>{{ $category }}</h2>
   <ul class="list-group" id="myUl">
     @foreach ($item as $service)
       <li class="list-group-item d-flex justify-content-between align-items-center border border-danger"><span  style="font-size:24px; color:red
      "><i class="bi bi-check-circle font-weight-bold "></i></span> <span style="font-weight:bold" id="service_category">{{ $service->service_category }}</span><span><u><i class="bi bi-magic"></i></u></span></li>
       <li class="list-group-item d-flex justify-content-between align-items-center"><span class="product_name" style="font-size:24px; color:#870FD1" data-price="349.34"><i class="bi bi-plus-circle"></i></span> <span class="product_name" id="service_name" data-price="{{ $service->service_price }}" data-category="{{ $service->service_category }}" >{{ $service->service_name }}</span><span class="text-success"><u>Available</u></span></li>
     @endforeach
   </ul>
@endforeach

You can grouping the list with php (in controller of blade into @php directive):

$list = [];
foreach($services as $item) {
  $list[$item->service_category][] = $item;
}

Updated method

public function order_services(){
    

    $services=  DB::table('services')
            ->selectRaw('id,service_name,service_category,service_price')
            ->orderBy('service_category')
            ->get();
            
    $list = [];
    foreach($services as $item) {
      $list[$item->service_category][] = $item;
    }
    
    return view('guests/services',['list'=>$list]);

}

And you can use $list in blade for display the result:

@foreach ($list as $category => $item)
  <h2>{{ $category }}</h2>
   <ul class="list-group" id="myUl">
     @foreach ($item as $service)
       <li class="list-group-item d-flex justify-content-between align-items-center border border-danger"><span  style="font-size:24px; color:red
      "><i class="bi bi-check-circle font-weight-bold "></i></span> <span style="font-weight:bold" id="service_category">{{ $service->service_category }}</span><span><u><i class="bi bi-magic"></i></u></span></li>
       <li class="list-group-item d-flex justify-content-between align-items-center"><span class="product_name" style="font-size:24px; color:#870FD1" data-price="349.34"><i class="bi bi-plus-circle"></i></span> <span class="product_name" id="service_name" data-price="{{ $service->service_price }}" data-category="{{ $service->service_category }}" >{{ $service->service_name }}</span><span class="text-success"><u>Available</u></span></li>
     @endforeach
   </ul>
@endforeach
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文