First of all I'm new at Laravel, I have already read some tutorials and a lot of the docs but now i am by an Problem where i can't find a solution.
I have three tables: post, post_adv_option, adv_option.
So the relationship between these tables would be:
- post to post_adv_option: One-to-Many
- post_adv_option to adv_option: One-to-One
Here is my code I am using:
Models
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 {
}
Tables
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 |
----------------------------------------
Controller
$postArray = Post::with('postAdvancedOptions')->where('id', '=', $id)->get()->toArray();
Result My current output looks something like this
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"
]
]
]
But what i need is:
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
]
]
]
How can I call the relation from Post with PostAdvancedOptions AND AdvancedOption
.. and order the post_advanced_options by AdvancedOption.sort