[英]Laravel eloquent relationship fetch records upto third level of relationship
我有用戶表,該表與vendordetails表具有hasOne關系。
class User extends Authenticatable
{
public function vendor_details() {
return $this->hasOne('App\Vendordetail');
}
}
另一方面,vendordetails表包含country_id,state_id和city_id,並且具有與國家,州和城市模型的belongsTo關系。
Vendordetail.php模型是:-
class Vendordetail extends Model
{
public $timestamps = false;
public function this_country(){
return $this->belongsTo('App\Country', 'country_id');
}
public function this_state(){
return $this->belongsTo('App\Country', 'state_id');
}
public function this_city(){
return $this->belongsTo('App\Country', 'city_id');
}
}
如果我在用戶表上查詢,如何獲取國家,州和城市的記錄。
$user = User::with('vendor_details')->first();
在此查詢中,它僅查找vendordetails表的結果,我還需要國家,州和城市。
謝謝
訪問嵌套關系
$user = User::with(['vendor_details.this_state','vendor_details.this_country','vendor_details.this_city'])->first();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.