ViralTiger of Viralbamboo Application Developers
9/28/2017 - 10:31 AM

Filtering a collection in Laravel

Removing items from a collection.

        // get your main collection with all the attributes...
        $activities = Activity::all();

        // build your second collection with a subset of attributes. this new
        // collection will be a collection of plain arrays, not Users models.
        $activities = $activities->map(function ($activity) {
            return collect($activity->toArray())
                ->only(['id', 'log_name', 'description'])
                ->all();
        });