【发布时间】:2021-03-10 09:20:10
【问题描述】:
试图在模型关系中集成“如果条件”。 但它不起作用。
$customer_index = Customer::where('firm_id', $firm_id)
->with(['company', 'tires', 'vehicles'])
->withCount(['tires', 'vehicles'])
->orderBy('id', 'desc');
我的 Customer.php 模型
public function vehicles()
{
if(isset($this->company->is_fleet)){
return $this->hasMany('App\Models\CustomerVehicle', 'fleet_id', 'company_id');
}
return $this->hasMany('App\Models\CustomerVehicle');
}
【问题讨论】:
-
你不能通过预先加载来做到这一点,因为在新实例(没有属性)上调用关系方法