我有这些模型
class Device extends Model
{
use HasFactory;
protected $connection = 'traccar';
public function vehicle()
{
//Code I need
}
}
class Vehicle extends Model
{
use HasFactory;
protected $connection = 'mysql';
}
class VehicleDevice extends Model
{
use HasFactory;
protected $connection = 'mysql';
}
正如你所看到的,设备在tracar数据库中,模型的重置在MySQL连接中。
如何编写设备与车辆的hasOneThrough关系?我得到一个错误,说
Table 'tracar.vehicles' doesn't exist (Connection: traccar, SQL: select * from `tc_devices` where exists (select * from `vehicles` inner join `vehicle_devices` on `vehicle_devices`.`id` = `vehicles`.`vehicle_device_id` where `tc_devices`.`id` = `vehicle_devices`.`device_id`))'
1条答案
按热度按时间biswetbf1#
通过定义此关系,您可以使用vehicle属性从Device示例访问关联的
Vehicle
模型。例如: