Chr*_*ond 1 pivot-table eloquent laravel-4
我正在建立一个汽车维修系统.每辆车都附有一个或多个部件.每个车辆和组件由帐户"拥有".用户可能希望将部件从车辆上拆下并将其放置在架子上以便以后重新连接到相同或另一车辆.该组件包含一个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'); }
}
Run Code Online (Sandbox Code Playgroud)
因此,我要查找的是找到属于指定account_id的所有组件,并且具有null 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');
}
Run Code Online (Sandbox Code Playgroud)
如果有类似"withPivot"方法的东西,那真的很酷......
$components = Component::where('vehicle_id', null)
->where('account_id', $account_id)
->withPivot('component_type_id', $vehicle_type_id);
Run Code Online (Sandbox Code Playgroud)
任何帮助都将不胜感激!提前致谢.
你的故事有点令人困惑,但我认为你正试图做这样的事情.答案是3个月的晚了,但既然你想要一个更优雅的方式,那就是.
实际上有一个像你建议的方法"withPivot".它的工作方式与您的建议略有不同.
- > withPivot('field1frompivottable','field2frompivottable','ect')
请参阅:http://laravel.com/docs/eloquent#working-with-pivot-tables
表的:
表:车辆
表:vehicletype
表:组件
表:vehicletypecomponentpivottable
雄辩的模特:
车型:
class Vehicle extends Eloquent {
protected $table = 'vehicle';
public function type() {
return $this->belongsTo('Vehicletype', 'type');
}
public function component() {
return $this->hasMany('Component', 'vehicle');
}
}
Run Code Online (Sandbox Code Playgroud)
车型型号:
class Vehicletype extends Eloquent {
protected $table = 'vehicletype';
public function vehicle() {
return $this->hasMany('Vehicle', 'type');
}
public function component() {
return $this->belongsToMany('Component', 'vehicletypecomponentpivottable', 'vehicletypeid', 'componentid')
->withPivot('installed');
}
}
Run Code Online (Sandbox Code Playgroud)
零件:
class Component extends Eloquent {
protected $table = 'component';
public function vehicle() {
return $this->belongsTo('Vehicle', 'vehicle');
}
public function vehicletype() {
return $this->belongsToMany('Vehicletype', 'vehicletypecomponentpivottable', 'componentid', 'vehicletypeid')
->withPivot('installed');
}
}
Run Code Online (Sandbox Code Playgroud)
控制器功能
public function get_uninstalled_components_that_fit_my_vehicle($vehicletypeid){
return Vehicletype::find($vehicletypeid)->component()->wherePivot('installed', 0)->get();
}
public function is_component_installed($componentid){
return Component::find($componentid)->Vehicletype()->pivot->installed;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!您可以使用wherePivot和过滤透视结果orWherePivot