Hello Neil, right now I'm in the same situation, have you achieve this? Any comment or suggestion would be greatly appreciated!
Mar 28, 2017
10
Level 5
Custom order of collection
I have used the merge() collection method to combine 3 different models collections that are Pages, Posts & Photos.
So now I have collection that looks like this...
['post', 'post', 'post', 'post', 'post', 'post', 'photo', 'photo', 'page', 'page']
Ideally I'd like to reorder this collection so that it's like...
['post', 'post', 'post', 'page', 'photo', 'photo', 'page', 'post', 'post', 'post']
Anybody have any idea what might be the best way to tackle this?
Thanks
Level 11
Dont use merge, split the post and page collection by count/2
$post = Post::all() // can use normal query builder to get the post you want
$post = $post->split($post->count() / 2)->toArray()
$page= Page::all(); // can use normal query builder to get the page you want
$page= $page->split($page->count() / 2)->toArray()
$photo = Photo::all(); // can use normal query builder to get the photo you want
$photo = $photo->toArray();
$collection = collect($post[0], $page[0], $photo, $page[1], $post[1])->collapse()->toArray();
dd($collection);
1 like
Please or to participate in this conversation.