2014-07-14 83 views
1

我創建4個數據庫表:
Laravel 4個查詢關係關係

工具
ID | NAME | TOOLTYPE_ID

工具類型
ID | NAME

工具分類
ID | NAME

Tool_Toolcategory
TOOL_ID | TOOLCATEGORY_ID

的車型如下:

class Tool extends Eloquent { 

public function toolCategories() 
{ 
    return $this->belongsToMany('ToolCategory', 'tool_toolcategory', 'tool_id', 'toolcategory_id'); 
} 

public function tooltype() 
{ 
    return $this->belongsTo('ToolType'); 
} 
} 

class ToolType extends Eloquent { 

public function tools() 
{ 
    return $this->hasMany('Tool', 'tooltype_id'); 
} 
} 

class ToolCategory extends Eloquent { 
public function tools() 
{ 
    return $this->belongsToMany('Tool', 'tool_toolcategory', 'tool_id', 'toolcategory_id'); 
} 
} 

好吧,我的問題就是如何讓基於創建工具與特定Tooltype刀具toolcategories。
示例我想要所有與類型爲「軟件」的工具相關的類別。
我有點失落,我已經看了想在我toolcategory模型使用範圍,做這樣的事情:

public function scopeHasType($query, $type) 
{ 
    return $query->whereHas('tools.tooltype', function($q) use ($type) 
    { 
    $q->where('name', '=', $type); 
    })->exists(); 
} 


這並沒有真正的工作:)然後我想這

$categories = ToolCategory::whereHas('tools.tooltype', function($query) 
{ 
    $query->where('tooltype_id', '=', 'Software');     
})->get(); 


再次沒有運氣。所以我希望這對某人有意義,他們可以把我推向正確的方向。先謝謝你。

回答

2

雄辯命名約定說:

  1. ModelNames是StudlyCased &奇異
  2. modelMethods被camelCased(關係動態屬性需要這個)
  3. table_names是snake_cased &複數
  4. table_columns被snake_cased
  5. pivot_table是model1_model2 snake_cased奇異(排名不分先後)

話雖這麼說,洋洋灑灑的模型ToolCategory查找表tool_categories

所以你需要指定,只要不是協定(單數,不是蛇套管等)表名:

// for example Tool model 
protected $table = 'tool'; 

而且你需要外鍵傳遞給關係定義,以便讓雄辯知道要尋找什麼。但是,你已經做了:

// ToolType model 
public function tools() 
{ 
    // here Eloquent would look for tool_type_id (based on related model) 
    return $this->hasMany('Tool', 'tooltype_id'); 
} 

// Tool model 
public function tooltype() 
{ 
    // here Eloquent looks for tooltype_id by default (based on relation name) 
    return $this->belongsTo('ToolType'); 
} 

現在,你的關係是確定用1個例外(按鍵錯誤的順序):

class ToolCategory extends Eloquent { 
public function tools() 
{ 
    return $this->belongsToMany('Tool', 'tool_toolcategory', 'toolcategory_id', 'tool_id'); 
} 

最後,whereHas不會對嵌套工作關係(但是,請檢查:https://github.com/laravel/framework/pull/4954),所以目前您需要:

// ToolCategory model 
public function scopeHasType($query, $type) 
{ 
    return $query->whereHas('tools', function ($q) use ($type) { 
    $q->whereHas('tooltype', function($q) use ($type) { 
     // use table prefix, since this is going to be join 
     $q->where('tooltype.name', '=', $type); 
    }); 
    }); 
} 
+0

偉大的嵌套作用域!也錯誤的關鍵順序,很好的發現:)非常感謝。 – parapark

+0

雄辯的命名約定小備忘單! –

0

有一些與您的代碼,第一關的問題,每個模型需要保護$表變量,這種方式:

class Tool extends Eloquent { 
    protected $table = 'tools'; 
    public function toolCategories(){ 
     return $this->belongsToMany('ToolCategory'); 
    } 

    public function tooltype(){ 
     return $this->belongsTo('ToolType'); 
    } 
} 

class ToolType extends Eloquent{ 
    protected $table = 'tooltypes'; 
    public function tools(){ 
     return $this->hasMany('Tool'); 
    } 
} 

class ToolCategory extends Eloquent{ 
    protected $table = 'toolcategories'; 
    public function tools(){ 
     return $this->belongsToMany('Tool'); 
    } 
} 

此外,您無法查詢以這種方式「model.related」關係,你需要寫嵌套關係查詢,像這樣:

public function scopeHasType($query, $type){ 
    return $query->whereHas('tools', function ($query) use ($type) { 
     $query->whereHas('tooltype', function($query2) use ($type) { 
      $query2->where('name', '=', $type); 
     }); 
    }); 
} 

而且,你quering錯列,如果你需要$類型爲字符串,您將得到查詢「名」而不是「tooltype_id ',如果你需要$ type作爲一個類型的實際ID,那麼你可以查詢'tooltype_id' 。

最後,不要忘記在表中定義索引,這個查詢可能會變得非常緩慢。