簡體   English   中英

使用雄辯的關系從dtabase中的多個表中顯示刀片中的數據

[英]Display data in blade from multiple table in dtabase using Eloquent relationship

我正在嘗試根據刀片中的相應數據顯示從多個表到單個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}})

我遇到錯誤,

嘗試獲取非對象的屬性“ district_name”(視圖:lapp / resources / views / area / index.blade.php

更改模型,使其了解外鍵

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');
}
$layout->districts()->first()->district_name

將顯示第一區。 如果有多個,則可以先將其更改為get()。 然后,您將需要另一個foreach。

您總是可以在查詢生成器上使用leftJoin select函數來獲得這些結果。 它也比每行調用它快得多。

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();

之后,您可以將選擇功能中的內容刷新為$layout->district_name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM