2014-10-20 78 views
1

首先,我是新的Laravel,我已經閱讀了一些教程和大量的文檔,但現在我遇到了一個問題,我找不到解決方案。Laravel多重關係

我有三個表:post,post_adv_option,adv_option。

那麼這些表之間的關係是:

  • 崗位post_adv_option:1對許多
  • post_adv_option到adv_option:壹壹到

這裏是我的代碼我正在使用:

型號

class Post extends Eloquent { 
    public function postAdvancedOptions() { 
     return $this->hasMany('PostAdvancedOptions','post_id'); 
    } 
} 

class PostAdvancedOption extends Eloquent { 
    public function AdvancedOption() { 
     return $this->hasOne('AdvancedOption','id','advanced_option_id'); 
    } 
} 

class AdvancedOption extends Eloquent { 

} 

Post Table 
------------------------------ 
| id | title | content | ... | 
------------------------------ 
| 1 | AAAAA | aaaaaaa | ... | 
------------------------------ 
| 2 | BBBBB | bbbbbbb | ... | 
------------------------------ 
| . | ..... | ....... | ... | 
------------------------------ 

PostAdvancedOption Table 
--------------------------------------------- 
| id | post_id | advanced_option_id | value | 
--------------------------------------------- 
| 1 | 1  | 2     | xxxxx | 
--------------------------------------------- 
| 2 | 2  | 1     | aaaaa | 
--------------------------------------------- 
| 3 | 2  | 4     | bbbbb | 
--------------------------------------------- 
| 4 | 2  | 5     | xxxxx | 
--------------------------------------------- 

AdvancedOption Table 
---------------------------------------- 
| id | name  | sort | description | 
---------------------------------------- 
| 1 | abc  | 0  | desc  | 
---------------------------------------- 
| 2 | def  | 2  | desc  | 
---------------------------------------- 
| 3 | ghi  | 1  | desc  | 
---------------------------------------- 
| . | ......... | .  | desc  | 
---------------------------------------- 
| 9 | mno  | 8  | desc  | 
---------------------------------------- 

控制器

$postArray = Post::with('postAdvancedOptions')->where('id', '=', $id)->get()->toArray(); 

結果 我的電流輸出看起來像這樣

array(3) [ 
array(15) [ 
    'id' => integer 64 
    'title' => string (19) "test" 
    'content' => string (6) "lorem ipsum"   
    'post_advanced_options' => array(3) [ 
     array(4) [ 
      'id' => integer 34 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "xxx" 
     ] 
     array(4) [ 
      'id' => integer 35 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "yyy" 
     ] 
     array(4) [ 
      'id' => integer 36 
      'post_id' => integer 64 
      'advanced_option_id' => integer 6 
      'option' => string (3) "vvv" 
     ] 
    ] 
] 

但我需要的是:由AdvancedOption.sort

array(3) [ 
array(15) [ 
    'id' => integer 64 
    'title' => string (19) "test" 
    'content' => string (6) "lorem ipsum"   
    'post_advanced_options' => array(3) [ 
     array(4) [ 
      'id' => integer 34 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "xxx" 

      'name' => string (3) "asd" 
      'description' => string (3) "asdasd" 
      'sort' => integer 0 

     ] 
     array(4) [ 
      'id' => integer 35 
      'post_id' => integer 64 
      'advanced_option_id' => integer 1 
      'option' => string (3) "yyy" 

      'name' => string (3) "asd" 
      'description' => string (3) "asdasd" 
      'sort' => integer 1 

     ] 
     array(4) [ 
      'id' => integer 36 
      'post_id' => integer 64 
      'advanced_option_id' => integer 6 
      'option' => string (3) "vvv" 

      'name' => string (3) "asd" 
      'description' => string (3) "asdasd" 
      'sort' => integer 1 
     ] 
    ] 
] 

我如何可以調用的關係從郵政PostAdvancedOptionsAdvancedOption

..並責令post_advanced_options

回答

3

目前你有你的桌子es建立了多對多關係,但您的模型沒有正確設置以建立多對多關係。您需要像這樣:

class Post extends Eloquent { 
    public function options() { 
     return $this->belongsToMany('AdvancedOption'); 
    } 
} 

class AdvancedOption extends Eloquent { 
    public function posts() { 
     return $this->belongsToMany('Post'); 
    } 
} 

你不需要爲PostAdvancedOptions一個模型,因爲那是你的數據透視表,而不是一個模型,但只是連接關係。每當你告訴Laravel一個型號belongsToMany它會自動查找某個數據透視表。該數據透視表將爲advanced_option_post,並且將具有id,advanced_option_idpost_id的列。如果你需要,你也可以重寫這個命名。

您可以閱讀更多關於多對多關係here

+1

而且你必須重視那些互相使用附加方法,例如:$ post-> AdvancedOption() - > attach(); – Burimi 2014-10-20 18:57:12

+0

@羅斯埃德曼,謝謝你的幫助解釋。我的解決方案現在是一個帶有函數的數據透視表:'返回$ this-> belongsToMany('AdvancedOption') - > orderBy(「sort」,「desc」) - > withPivot(array('value'));' – Patrick 2014-10-21 09:31:17

0

我也相對新Laravel,但如果你只需要AdvancedOptions信息,你可以在帖子模型中使用了「Has Many Through」:

class Post extends Eloquent { 
    public function AdvancedOptions() { 
     return $this->hasManyThrough('PostAdvancedOption', 'AdvancedOption'); 
    } 
} 

或者,如果你要實現的數據透視表如Ross Edman所述,那麼你應該將PostAdvancedOption表中的「值」移動到AdvancedOption

0

如果關係是一對多關係,那麼可以將外鍵保留在子表中(在這種情況下,AdvanceOptions保存郵政主鍵)。

既然你定義這樣的關係,我建議修復數據庫架構,則AdvanceOption保存這樣父郵政主鍵:

Post Table 
------------------------------ 
| id | title | content | ... | 
------------------------------ 
| 1 | AAAAA | aaaaaaa | ... | 
------------------------------ 



AdvancedOption Table 
------------------------------------------------------------------- 
| id | name  | post_id | value | another advanceoption column | 
------------------------------------------------------------------- 
| 1 | abc  | 0  |  |        | 
------------------------------------------------------------------- 

,然後將模型看起來是這樣的:

class Post extends Eloquent { 
    public function advanceoption() { 
     return $this->hasMany('App\AdvanceOption','post_id'); 
    } 
} 

class AdvancedOption extends Eloquent { 
    public function post() { 
     return $this->belongsTo('App\Post','post_id'); 
    } 
} 

終於,找到你想要的結果:

$postArray = Post::find($id)->advanceoption()->get();