classes/Models/Traits/SerializeWithImages.php
protected function getRelatedModelsWithImages($parents, $relation)
    {
        $models = [];

        foreach ($parents as $parent) {
            // Get the related object
            $related = $parent->$relation;

            // The related object is a model (like in a belongsTo setup), add it to
            // the array
            if (is_a($related, Model::class)) {
                $models[] = $related;
            }

            // Otherwise, if the relation returns a collection (like a hasMany), add
            // all the related models to the growing array.
            elseif (is_a($related, Collection::class)) {
                $models = array_merge($models, $related->all());
            }
        }

        return $models;
    }
protected function getRelatedModelsWithImages($parents, $relation)
    {
        $models = [];

        foreach ($parents as $parent) {
            // Get the related object
            $related = $parent->$relation;

            // The related object is a model (like in a belongsTo setup), add it to
            // the array
            if (is_a($related, Model::class)) {
                $models[] = $related;
            }

            // Otherwise, if the relation returns a collection (like a hasMany), add
            // all the related models to the growing array.
            elseif (is_a($related, Collection::class)) {
                $models = array_merge($models, $related->all());
            }
        }

        return $models;
    }
protected function getRelatedModelsWithImages ( $ parents , $ relation ) { $ models = [ ] ; foreach ( $ parents as $ parent ) { // Get the related object $ related = $ parent -> $ relation ; // The related object is a model (like in a belongsTo setup), add it to // the array if ( is_a ( $ related , Model :: class ) ) { $ models [ ] = $ related ; } // Otherwise, if the relation returns a collection (like a hasMany), add // all the related models to the growing array. elseif ( is_a ( $ related , Collection :: class ) ) { $ models = array_merge ( $ models , $ related -> all ( ) ) ; } } return $ models ; }
Take an array of parent models and return a new array of the children of
all parents in one array

@param  array  $parents  Array of eloquent models
@param  string $relation Relation name
@return array
Take an array of parent models and return a new array of the children of all parents in one array