我正在建立一个汽车维修系统。每辆车都有一个或多个附件。每个车辆和组件都由一个账户“拥有”。用户可能希望从车辆上拆下部件并将其放置在架子上,以便以后重新连接到同一辆或另一辆车上。该组件拥有一个vehicle_id字段,因此拆卸非常容易,我只需将vehicle_id字段置空,并且组件被有效“搁置”。先进的雄辩关系与枢轴?
我的困难在于找到可连接的组件来显示可连接到选定车辆的搁架部件列表。 vehicle_types_component_types表是定义哪些组件类型可以附加到哪些车型的枢轴。某些组件类型可以安装在多种车型上,每种车型都有一种或多种可以安装的组件类型。
class VehicleType extends Eloquent {
public function ComponentTypes() { return $this->belongsToMany('ComponentType', 'vehicle_types_component_types')->withTimestamps(); }
}
class Vehicle extends Eloquent {
public function Account() { return $this->belongsTo('Account', 'account_id'); }
public function VehicleType() { return $this->belongsTo('VehicleType', 'vehicle_type_id'); }
public function Components() { return $this->hasMany('Component'); }
}
class ComponentType extends Eloquent {
public function VehicleTypes() { return $this->belongsToMany('VehicleTypes', 'vehicle_types_component_types')->withTimestamps(); }
public function Components() { return $this->hasMany('Component'); }
}
class Component extends Eloquent {
public function Account() { return $this->belongsTo('Account', 'account_id'); }
public function Vehicle() { return $this->belongsTo('Vehicle', 'vehicle_id'); } // This is null when component is not attached to vehicle
public function ComponentType() { return $this->belongsTo('ComponentType', 'component_type_id'); }
}
所以说我希望做的是找到属于指定ACCOUNT_ID的所有组件,并且有一个空vehicle_id,并在组件的组件类型的车辆类型为“安装”中的定义, vehicle_types_component_types数据透视表。最重要的是,我还需要通过其组件类型排除某些组件......例如,component_type!='tire'。
我已经试过了显然是行不通的,但应证明什么,我试图做...
public function findAttachable($vehicle_type_id, $account_id)
{
return Component::with('ComponentType')
->where('vehicle_id', null)
->where('account_id', $account_id)
->where('vehicle_type_component_type.vehicle_type_id', $vehicle_type_id)
->where('vehicle_type_component_type.component_type_id', 'components.components_type_id')
->where('component_types.type', '!=', 'tire');
}
如果有东西像“withPivot”方法什么的,那会真的很酷...
$components = Component::where('vehicle_id', null)
->where('account_id', $account_id)
->withPivot('component_type_id', $vehicle_type_id);
任何帮助,将不胜感激!提前致谢。