This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Tschallacka
Let's say we have the following data structure
Blog:
RelationBlogSections:
Blogsection1
sortorder: 1
text: bla
Blogsection3
sortorder: 2
text: bleh
Blogsection2
sortorder: 3
text: theEnd
You want the relation to have it's records neatly sorted so you can just iterate through it with a foreach
You can do this with a 'subquery' on the relation when doing a new select
$model = Blog::with([
'blogsections'=>
function($query) {
$query->
withPivot('sortorder')->
orderBy('sortorder','asc');
}
])->
find($KEY);
In this example blogsections is the name of the relation as defined in your model definition file, and pivot is the name of your pivot table.
public $belongsToMany = ['blogsections' => ['\Tschallacka\Blogmanaer\Models\BlogSection',
'key' =>'blogpage_id',
'otherKey'=>'blog_section_id',
'table' => 'blog_section_to_page',
'order' => 'sortorder',
'pivot'=>'sortorder',
'timestamps'=>'true']];
Last updated
1-1 of 1