Collection::orderBy does not exist on concat

Posted 3 months 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.

Reply to

Use Markdown with GitHub-flavored code blocks.