jeudi 29 décembre 2016

How to properly merge more than 2 Eloquent Collections in Laravel 5.2?

My issue right now is whenever I create two new assignments, the last Post in the merged collection disappears.

// Here is the code: 
@foreach($user->groups as $group)
    <?php
    $all = $group->posts->merge($group->assignments)->merge($group->quizzes);

    $sorted = $all->sortByDesc('created_at');
    ?>
        @foreach($sorted as $item)
        <?php
        $val = get_class($item);
            switch ($val)
        {
            case 'App\Post':
            ?>
                Post:<br>    
            <?php
                break;
            case 'App\Assignment':
                ?>
                Assignment:<br>

                <?php
                break;
            case 'App\Quiz':
            ?>
                Quiz:<br>

            <?php
                break;
        }
        ?>
    @endforeach
 @endforeach

So how can I properly merge various Eloquent Collections together, then sort them by the latest?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire