Laravel 4 与pivot的高级口才关系?
我正在建立一个车辆维修系统。每辆车都连接有一个或多个部件。每个车辆和部件都由一个帐户拥有。用户可能希望从车辆上拆下部件,并将其放在托架上,以便以后重新连接到同一车辆或另一车辆上。该组件包含一个vehicle_id字段,因此分离很容易,我只需将vehicle_id字段设置为null,该组件就被有效地搁置起来 我的困难在于查找可连接部件以显示可连接到选定车辆的搁置部件列表。“车辆类型”“部件类型”表是一个轴,用于定义哪些部件类型可以附着到哪些车辆类型。某些部件类型可以安装在多种车型上,并且每种车型都有一种或多种可安装的部件类型Laravel 4 与pivot的高级口才关系?,laravel-4,pivot-table,eloquent,Laravel 4,Pivot Table,Eloquent,我正在建立一个车辆维修系统。每辆车都连接有一个或多个部件。每个车辆和部件都由一个帐户拥有。用户可能希望从车辆上拆下部件,并将其放在托架上,以便以后重新连接到同一车辆或另一车辆上。该组件包含一个vehicle_id字段,因此分离很容易,我只需将vehicle_id字段设置为null,该组件就被有效地搁置起来 我的困难在于查找可连接部件以显示可连接到选定车辆的搁置部件列表。“车辆类型”“部件类型”表是一个轴,用于定义哪些部件类型可以附着到哪些车辆类型。某些部件类型可以安装在多种车型上,并且每种车型都
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'); }
}
因此,我要做的是查找属于指定帐户id的所有组件,并且具有空车辆id,并且该组件的组件类型可安装在车辆类型上,如车辆类型组件类型透视表中所定义。最重要的是,我还需要根据组件类型排除某些组件。。。其中组件类型!='例如,轮胎
我所尝试的显然不起作用,但应该证明我正在尝试做什么
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);
在此方面的任何帮助都将不胜感激!提前谢谢。我找到了一个似乎有效的解决方案。我只是希望有一个更优雅的方式来做这件事。如果有,请告诉我
public function findAttachable($vehicle_type_id, $account_id)
{
return Component::with('ComponentType')
->where('vehicle_id', null)
->where('account_id', $account_id)
->whereIn('component_type_id', function($query) use ($vehicle_type_id) {
$query->select('component_type_id')
->from('vehicle_types_component_types')
->where('vehicle_type_id', $vehicle_type_id);
})
->whereNotIn('component_type_id', function($query) {
$query->select('id')
->from('component_types')
->where('type', 'tire');
})
->get();
}
我找到了一个似乎有效的解决方案。我只是希望有一个更优雅的方式来做这件事。如果有,请告诉我
public function findAttachable($vehicle_type_id, $account_id)
{
return Component::with('ComponentType')
->where('vehicle_id', null)
->where('account_id', $account_id)
->whereIn('component_type_id', function($query) use ($vehicle_type_id) {
$query->select('component_type_id')
->from('vehicle_types_component_types')
->where('vehicle_type_id', $vehicle_type_id);
})
->whereNotIn('component_type_id', function($query) {
$query->select('id')
->from('component_types')
->where('type', 'tire');
})
->get();
}
介绍
你的故事有点让人困惑,但我认为你是在尝试这样做。
答案是晚了3个月,但既然你想要一个更优雅的方式,它就是
实际上,有一种方法使用Pivot,就像您建议的那样。它的工作原理与您建议的略有不同
->使用Pivot'field1frompivottable','field2frompivottable','ect'
见:
实例
表1:
表:车辆
身份证件
名称
类型
表:车辆类型
身份证件
名称
表:组成部分
身份证件
名称
交通工具
表:车辆类型组件数据透视表
身份证件
车辆类型ID
组件
安装
雄辩的模型:
车辆型号:
class Vehicle extends Eloquent {
protected $table = 'vehicle';
public function type() {
return $this->belongsTo('Vehicletype', 'type');
}
public function component() {
return $this->hasMany('Component', 'vehicle');
}
}
车型:
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');
}
}
组成部分:
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');
}
}
控制器功能
祝你好运
希望这有帮助!您可以使用Where pivot和或Where pivot对pivot结果进行筛选
你的故事有点让人困惑,但我认为你是在尝试这样做。
答案是晚了3个月,但既然你想要一个更优雅的方式,它就是
实际上,有一种方法使用Pivot,就像您建议的那样。它的工作原理与您建议的略有不同
->使用Pivot'field1frompivottable','field2frompivottable','ect'
见:
实例
表1:
表:车辆
身份证件
名称
类型
表:车辆类型
身份证件
名称
表:组成部分
身份证件
名称
交通工具
表:车辆类型组件数据透视表
身份证件
车辆类型ID
组件
安装
雄辩的模型:
车辆型号:
class Vehicle extends Eloquent {
protected $table = 'vehicle';
public function type() {
return $this->belongsTo('Vehicletype', 'type');
}
public function component() {
return $this->hasMany('Component', 'vehicle');
}
}
车型:
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');
}
}
组成部分:
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');
}
}
控制器功能
祝你好运
希望这有帮助!您可以使用wherePivot和或wherePivot对pivot结果进行过滤