【问题标题】:Laravel7 2 Table joining with complex dataLaravel 7 2 表连接复杂数据
【发布时间】:2021-08-21 03:23:19
【问题描述】:

我想显示来自 tbl_structure 的数据和来自 tble_peple 的人名,但我不知道如何加入它。我是 laravel 的新手。

my tbl_structure

my tble_peple

showing data

我在控制器上的代码

public function get_people(){
 $Showstructe= DB::table('tbl_structure')->orderby("name","asc")
 ->get();
 return view('admin.TestCustomer.structure_show',compact('Showstructe'));
}

我的代码来显示数据

<table class="table table-bordered">
 <tr>
  <th>ID</th>
  <th>Structure</th>
  <th>Stage A</th>
  <th>Stage B</th>
  <th>Stage C</th>
  <th>Stage D</th>
  <th>Stage E</th>
  <th>Stage F</th>
</tr>
@foreach($Showstructe as $people)
<tr>
  <td>{{ $people->id }}</td>
  <td>{{ $people->name }}</td>
  <td>{{ $people->stage_a }}</td>
  <td>{{ $people->stage_b }}</td>
  <td>{{ $people->stage_c }}</td>
  <td>{{ $people->stage_d }}</td>
  <td>{{ $people->stage_e }}</td>
  <td>{{ $people->stage_f }}</td>
<tr>
@endforeach
</table>

如何显示人名而不仅仅是人名?

【问题讨论】:

    标签: laravel-7


    【解决方案1】:
        public function get_people(){
         $Showstructe= DB::table('tbl_structure')->leftJoin('people as p', 'tbl_structure.id', '=', 'p.id')->select('p.name as peopleName')->orderby("name","asc")
         ->get();
         return view('admin.TestCustomer.structure_show',compact('Showstructe'));
        }
    
    
    <table class="table table-bordered">
     <tr>
      <th>ID</th>
      <th>Structure</th>
      <th>Stage A</th>
      <th>Stage B</th>
      <th>Stage C</th>
      <th>Stage D</th>
      <th>Stage E</th>
      <th>Stage F</th>
    </tr>
    @foreach($Showstructe as $people)
    <tr>
      <td>{{ $people->peopleName}}</td>
      <td>{{ $people->name }}</td>
      <td>{{ $people->stage_a }}</td>
      <td>{{ $people->stage_b }}</td>
      <td>{{ $people->stage_c }}</td>
      <td>{{ $people->stage_d }}</td>
      <td>{{ $people->stage_e }}</td>
      <td>{{ $people->stage_f }}</td>
    <tr>
    @endforeach
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2021-02-21
      • 1970-01-01
      相关资源
      最近更新 更多