2013-01-20 121 views
1

如何找到與Eloquent ORM關係的關係?目前我有這樣的事情。簡單的關係。我可以找到Image和它的攝影師。現在我需要做更復雜的事情,我還需要找到攝影師的標籤。Laravel爲關係添加/找到關係

轉儲看起來像這樣

object(Image) { 
    ["attributes"] => [], 
    ["relationships"] => 
     ["photographer"] => 
      ["attributes"] => [], 
      ["relationships"] => 
} 

但我需要添加標籤的關係,以便它看起來像這樣

object(Image) { 
    ["attributes"] => [], 
    ["relationships"] => 
     ["photographer"] => 
      ["attributes"] => [], 
      ["relationships"] => 
       ["tags"] => 
        ["attributes"] => [], 
        ["relationships"] => 
} 

這怎麼可能?

/圖片模式

public function photographer() 
{ 
    return $this->belongs_to('Photographer'); 
} 

public function tags() 
{ 
    return $this->has_many_and_belongs_to('Tag', 'tag_relationships'); 
} 

/控制器

$images = Image::with(['photographer'])->order_by('updated_at', 'desc')->get(); 
+0

得到在IRC答案。也許這將工作Image :: with(['photographer','photographer.tags']),明天會測試它。 –

回答

3

你只是用laravel的點語法:

Image::with(['photographer', 'photographer.tags', 'photographer.tags.categories]) .... 
+0

這可能不在文檔中嗎? –

+0

它是 - [Eloquent ORM - Eager Loading](http://laravel.com/docs/database/eloquent#eager) – Bartek