我有 users 表,它与 vendordetails 表有 hasOne 关系。
class User extends Authenticatable
{
public function vendor_details() {
return $this->hasOne('App\Vendordetail');
}
}
Run Code Online (Sandbox Code Playgroud)
另一方面,vendordetails 表包含 country_id、state_id 和 city_id,并且与 Country、State 和 City 模型具有关联关系。
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');
}
}
Run Code Online (Sandbox Code Playgroud)
如果我查询用户表,我如何获取国家、州和城市的记录。
$user = User::with('vendor_details')->first();
Run Code Online (Sandbox Code Playgroud)
在这个查询中,它只找到 vendordetails 表的结果,我还想要国家、州和城市。
谢谢