Collection::orderBy does not exist on concat

Posted 1 month ago by splendidkeen

I am trying to get two Models, concat them to a collection, and order them within the concatenation by updated_at, to create an already sorted (by updated_at) foreach loop within my view, and just check on the Model instance, then trigger a partial based on the Model.

$users = $partner->users()->get();
$events = $partner->events()->get();

$collection = $users->concat($events)->orderBy('updated_at', 'desc')->limit(5)->get();

This results in Collection::orderBy does not exist

Logic within my view:

@foreach($collection as $object)
    
    @if($object instanceof App\Models\User)

        // User related partial

    @elseif($object instanceof App\Models\Event)
        
        // Event related partial
    
    @endif

@endforeach


How can I order the concatenated Collection by updated_at ?

Limit on a collection also won't work. Is the thought even possible?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.