我正在尝试添加与我的 Booking
模型的关系。我已经有一个如下所示的关系:
public function vehicle() {
return $this->belongsTo(Vehicle::class);
}
适用于 bookings.vehicle_id
列。不过,现在我添加了另一列名为 bookings.billed_vehicle_id
的列,如果 vehicle_id
不为空,它将“覆盖”它。所以我想做这样的事情:
public function billedVehicle() {
return $this->belongsTo(Vehicle::class,DB::raw('coalesce(billed_vehicle_id,vehicle_id)'));
}
这可能吗?
如果 belongsTo
不可能,我可以以某种方式创建一个支持此功能的自定义关系吗?
最佳答案
belongsTo()
返回一个查询生成器,因此您可以继续将 ->whereRaw()
链接到其末尾:
public function billedVehicle() {
return $this->belongsTo(Vehicle::class)
->whereRaw(DB::raw('coalesce(billed_vehicle_id,vehicle_id)'));
}
关于php - Laravel 属于合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48067159/