【问题标题】:Display data in blade from multiple table in dtabase using Eloquent relationship使用 Eloquent 关系在数据库中的多个表中显示刀片中的数据
【发布时间】:2019-02-13 17:28:23
【问题描述】:

我正在尝试根据刀片中的相应数据将多个表中的字段显示到单个 html 表中。但我没有得到任何结果

我的数据库设计:

District 
| id 
| district_name

Municipal
| id
| district_id
| Municipal_uid
| municipal_name 

Area
| id 
| area_name
| district_id
| municipal_id

这就是我想要达到的目标,

Area ID | Area Name | District Name | Municipal Name | municipal UID

我的模特

地区:

public function districts(){
    return $this->belongsTo('App\Districts');
}

public function municipals(){
    return $this->belongsTo('App\Municipals');
}

市政:

public function district(){
    return $this->belongsTo('App\Districts');
}

public function approvedlayout(){
    return $this->hasMany('App\Approvedlayouts');
}

地区:

 public function municipal(){
    return $this->hasMany('App\Municipals');
}

public function approvedlayout(){
    return $this->hasMany('App\Approvedlayouts');
}

刀片

 <table class="table table-striped">
                            <thead class="text-center">
                                <tr>
                                    <th>Area ID</th>
                                    <th>Area Name</th>
                                    <th>District  Name </th>
                                    <th>Municipal Name</th>
                                    <th>Municipal UID</th>
                                </tr>
                            </thead>

                            <tbody class="list">
                                @foreach ($areas as $layout)
                                <tr>
                                    <td>{{$layout ->id}}</td>
                                    <td> {{ $layout-area_name }}</td>
                                    <td> {{ $layout->districts-> district_name }}</td>
                                    <td> </td>
                                    <td></td>
                                    <td></td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                        {{$areas-> links()}} 

控制器

  public function index()
    {
        $areas = Area::simplePaginate(5);
        return view('admin.area.index',compact('areas'));
    }

当我尝试显示区名时

( {{ $layout->districts-> District_name }})

我遇到了错误,

试图获取非对象的属性“区名”(查看: lapp/resources/views/area/index.blade.php

【问题讨论】:

    标签: php database laravel eloquent


    【解决方案1】:

    改变你的模型,让它知道外键

    Area:
    
    public function districts(){
        return $this->belongsTo('App\Districts','district_id','id');
    }
    
    public function municipals(){
        return $this->belongsTo('App\Municipals','Municipal_uid','id');
    }
    
    Municipal:
    
    public function district(){
        return $this->belongsTo('App\Districts','district_id','id');
    }
    
    
    District:
    
     public function municipal(){
        return $this->hasMany('App\Municipals','id','district_id');
    }
    

    【讨论】:

    • 谢谢@Mubbashar,:)
    【解决方案2】:
    $layout->districts()->first()->district_name
    

    将显示第一区。 如果有多个,可以先改成get()。然后你需要另一个 foreach。

    【讨论】:

    • 根据schema,一个布局(区域)怎么可能有多个区域?
    • "公共功能区S()"
    【解决方案3】:

    您总是可以在查询构建器上使用leftJoinselect 函数来获得这些结果。它也比按行调用要快得多。

    Area::select('areas.id','areas.name', 'districts.name as district_name', 'municipals.name as municipal_name', 'municipals.id as municipal_id')
        ->leftJoin('districts', 'districts.id', '=', 'area.district_id')
        ->leftJoin('municipals', 'municipals', '=', 'area.municipal_id')
        ->get();
    

    之后,您可以将select函数中的东西刷新为$layout-&gt;district_name

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 2022-08-14
      • 2021-01-07
      • 2021-09-22
      • 2021-05-19
      • 2016-04-04
      相关资源
      最近更新 更多