gpt4 book ai didi

php - Laravel 属于合并?

转载 作者:行者123 更新时间:2023-11-29 01:50:04 24 4
gpt4 key购买 nike

我正在尝试向我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com