首先,我是新的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