在laravel中显示父项的名称

时间:2019-05-08 14:42:16

标签: laravel-5.7

我试图在下面的服务表中显示服务。

Services table

我希望父ID为零的服务在父ID列中显示___,因为它们是父服务,而parent_id值大于零的那些服务将与父名称一起显示服务(s_name)。

到目前为止,这是我的控制器代码

    public function viewServices() {
    if (Session::has('adminSession')) {
        $services = Service::get();
        foreach ($services as $service) {
            if ($service->parent_id == 0) {
                $parent_id = "__";
            } else {
                $parent_id = Service::where(['parent_id' => 0])->pluck('s_name');
            }
        }
        return view('admin.service.view_services')->with(compact('services', 'parent_id'));
    } else {
        return redirect('/admin')->with('flash_message_error', 'Access denied! Please Login first');
      }
  }

这是视图代码

    <tbody>
     @foreach($services as $service)
          <tr class="gradeX">
             <td class="text-center">{{ $service->id }}</td>
             <td class="text-center">{{ $service->s_name }}</td>
             <td class="text-center">{{ $parent_id }}</td>
             <td class="text-center">{{ $service->description }}</td>
             <td><a href="#productModal{{ $service->id }}" data-toggle="modal" class="btn btn-success btn-mini">View <i class="icon icon-eye-open"></i></a> | <a href="{{url('admin/edit_service/'.$service->id)}}" class="btn btn-primary btn-mini">Edit <i class="icon icon-edit"></i></a> | <a rel="{{$service->id}}" rel1="delete_service" href="javascript:" class="btn btn-danger btn-mini deleteService">Delete <i class="icon icon-trash"></i></a></td>
                  </tr>
      <div id="productModal{{ $service->id }}" class="modal hide">
        <div class="modal-header bg-primary">
         <button data-dismiss="modal" class="close" type="button">×</button>
         <h3 class="text-center">{{ $service->s_name}}</h3>
       </div>
       <div class="modal-body">
          <p class="text-center">PARENT: >{{ $parent_id  }}</p>
          <p class="text-center bg-primary">DESCRIPTION: {{$service->description}}</p>
               </div>
             </div>
              @endforeach
            </tbody>

这给了我这个 Service View

现在,您可以看到所有父服务都显示在每个级别中,其次,拔出的输出未正确显示。我如何正确获取和显示所有父服务? 谢谢

0 个答案:

没有答案