请求的表格如下:
**User Table**
id, name
1, vehicle person name
2, renter person name
**Vehicle Table**
id, user_id, vehicle_name
1, 1, My car
**Booking Table**
id, renter_id, vehicle_id
1, 2, 1
用户模型
public function renter() {
return $this->hasMany(Booking::class, 'renter_id');
}
public function vehicleBook() {
return $this->hasManyThrough(Booking::class, Vehicle::class);
}
预订模式
public function user() {
return $this->belongsTo(User::class, 'renter_id');
}
车辆型号
public function user() {
return $this->belongsTo(User::class);
}
我的控制器
$renters = Auth::user()->renter()->get();
$owners = Auth::user()->vehicleBook()->get();
// In Loop
$renter->user->name; // renter person name
$owner->user->name; // vehicle person name
结果
根据预订表格,我想使用Laravel 5 ORM获得租房人和车辆人名。
我已经使用两个电话完成了这项工作,但我想知道是否有办法通过一次通话获得结果?
答案 0 :(得分:0)
你可以这样做。它会减少行数,但会增加查询次数。
$user = User::find(Auth::user()->id)->with('renters')->with('vehicleBooks')->first();