0

I have an array storing 3 merged collections:

public function answer() {
        $interview = Interview::find(1);

        $videoQuestions = $interview->VideoQuestions()->get();
        foreach ($videoQuestions as $vq) {
            $vq->type = 'video';
        }

        $textQuestions = $interview->TextQuestions()->get();
        foreach ($textQuestions as $tq) {
            $tq->type = 'text';
        }

        $uploadQuestions = $interview->UploadQuestions()->get();
        foreach ($uploadQuestions as $uq) {
            $uq->type = 'upload';
        }

        $questions = collect($videoQuestions);
        $questions = $questions->merge($textQuestions);
        $questions = $questions->merge($uploadQuestions);

        $questions->sortBy('order_position');
        dd($questions);
    }

The ->sortBy is not working as I assume it doesn't work on an array of collections.

This is what I have: enter image description here

But I really just want it to be a merged collection not an array inside the collection.

Each VideoQuestion or TextQuestion has an order_position field that I would like to sort by.

Any help is appreciated!

2 Answers 2

2

First thing collection is a class containing an array of elements with special methods to filter sort, docs:

The Illuminate\Support\Collection class provides a fluent, convenient wrapper for working with arrays of data.

The sortBy method returns a sorted collection (it does not apply to the item it has):

$collection = collect([
    ['name' => 'Desk', 'price' => 200],
    ['name' => 'Chair', 'price' => 100],
    ['name' => 'Bookcase', 'price' => 150],
]);

$sorted = $collection->sortBy('price');

$sorted->values()->all();

/*
    [
        ['name' => 'Chair', 'price' => 100],
        ['name' => 'Bookcase', 'price' => 150],
        ['name' => 'Desk', 'price' => 200],
    ]
*/

So you should have something like:

$questions = $questions->sortBy('order_position');
dd($questions);
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I simply needed to change $questions->sortBy('order_position'); to $questions = $questions->sortBy('order_position'); Thanks!
0

This could be an alternative:

$interview = Interview::find(1)->with(['VideoQuestions',
                                       'TextQuestions', 
                                       'UploadQuestions', 
                                       'Questions' => function($query){
                                           $query->orderBy('order_position', 'asc');
                                       }])->get();

Define the relationships and make a table Questions that holds an order_position that you can sort on instead of each table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.