spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.76k stars 1.07k forks source link

Migration from V4 to V5 - getMedia method #1198

Closed mistre83 closed 6 years ago

mistre83 commented 6 years ago

In version 4 of Media library the method getMedia returned all the collections. So, if my object has 3 collections, that method return all.

Now, in version 5 the getMedia have as default parameter default.

There is a way to get all the media as in version 4?

The only trick i've found is to call getMedia with empty string

->getMedia('')

Thanks

freekmurze commented 6 years ago

Use your own model and add the methods you need there: https://docs.spatie.be/laravel-medialibrary/v7/advanced-usage/using-your-own-model

jaumesala commented 5 years ago

Or you can just call the relationship:

$post = Post::find(1);
$items = $post->media;

// $items is a Illuminate\Database\Eloquent\Collection instance

The core of the HasMediaTrait relies on that polymorphic relationship:

/**
 * Set the polymorphic relation.
 *
 * @return \Illuminate\Database\Eloquent\Relations\MorphMany
 */
public function media()
{
    return $this->morphMany(config('medialibrary.media_model'), 'model');
}

The method getMeida()only filters the collection.