mercredi 8 mars 2017

Create a new and custom collection

In my app im getting a array of objects, from the table, and in the table i get the information regarding each object, but i need to create a new collection with specific name properties but with the values from the collection i went and get. How can i create a new custom collection from the old one i got from DB?

my code:

$conditions = collect($user->conditions()
->withTrashed()
->orderBy('updated_at', 'desc')
->get());

Example of how i was trying do do:

$newCollection =  $conditions->map(function ($condition) {
           unset($condition['condition_privacy']);
           unset($condition['condition_privacy_id']);
           unset($condition['discount_type']);
           unset($condition['id']);
           unset($condition['condition_privacy']);
           unset($condition['proponent']);
           unset($condition['simplified_laboratory']);
           unset($condition['simplified_laboratory_id']);
           $condition['new_property'] = 'some data';
            return $condition;
        });

But this way above is not quite good, i prefer in instead removing the properties from the original, i wish to create a new collection with new properties and getting values from the old collections.

note: Im using laravel 5.1.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire