首先,我是新的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
]
]
]
我如何可以調用的關係從郵政與PostAdvancedOptions和AdvancedOption
..並責令post_advanced_options
而且你必須重視那些互相使用附加方法,例如:$ post-> AdvancedOption() - > attach(); – Burimi 2014-10-20 18:57:12
@羅斯埃德曼,謝謝你的幫助解釋。我的解決方案現在是一個帶有函數的數據透視表:'返回$ this-> belongsToMany('AdvancedOption') - > orderBy(「sort」,「desc」) - > withPivot(array('value'));' – Patrick 2014-10-21 09:31:17