2013-11-20 80 views
1

我正在建立一個汽車維修系統。每輛車都有一個或多個附件。每個車輛和組件都由一個賬戶「擁有」。用戶可能希望從車輛上拆下部件並將其放置在架子上,以便以後重新連接到同一輛或另一輛車上。該組件擁有一個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); 

任何幫助,將不勝感激!提前致謝。

回答

0

我找到了一個似乎可行的解決方案。我只希望有一個更優雅的方式來做到這一點。如果有,請告訴我。

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(); 
} 
8

介紹

你的故事略顯混亂,但我認爲你正在嘗試做這樣的事情。 答案是3個月的晚,但因爲你想要一個更優雅的方式在這裏。

實際上有一種方法「withPivot」就像你建議的那樣。它的工作原理與您所建議的略有不同。

- > withPivot( 'field1frompivottable', 'field2frompivottable', 'ECT')

參見:http://laravel.com/docs/eloquent#working-with-pivot-tables

表的:

表:車輛

  • ID
  • 名稱
  • 類型

表:vehicletype

  • ID
  • 名稱

表:成分

  • ID
  • 車輛

表:vehicletypecomponentpivottable

  • ID
  • vehicletypeid
  • 的ComponentID
  • 在停滯

鋒型號:

車輛模型:

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'); 
    } 

} 

控制器功能

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; 
} 

祝你好運!

希望這有助於!您可以使用wherePivotorWherePivot