Working with collections | laravel-medialibrary | Spatie

 SPATIE

  Laravel Media Library
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-medialibrary](https://spatie.be/docs/laravel-medialibrary/v5)  Basic-usage  Working with collections

 Version   v11   v10   v9   v8   v7   v6   v5   v4   v3

 Other versions for crawler [v11](https://spatie.be/docs/laravel-medialibrary/v11) [v10](https://spatie.be/docs/laravel-medialibrary/v10) [v9](https://spatie.be/docs/laravel-medialibrary/v9) [v8](https://spatie.be/docs/laravel-medialibrary/v8) [v7](https://spatie.be/docs/laravel-medialibrary/v7) [v6](https://spatie.be/docs/laravel-medialibrary/v6) [v5](https://spatie.be/docs/laravel-medialibrary/v5) [v4](https://spatie.be/docs/laravel-medialibrary/v4) [v3](https://spatie.be/docs/laravel-medialibrary/v3)

- [ Introduction ](https://spatie.be/docs/laravel-medialibrary/v5/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-medialibrary/v5/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-medialibrary/v5/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-medialibrary/v5/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-medialibrary/v5/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-medialibrary/v5/changelog)
- [ About us ](https://spatie.be/docs/laravel-medialibrary/v5/about-us)

Basic usage
-----------

- [ Preparing your model ](https://spatie.be/docs/laravel-medialibrary/v5/basic-usage/preparing-your-model)
- [ Associating files ](https://spatie.be/docs/laravel-medialibrary/v5/basic-usage/associating-files)
- [ Retrieving media ](https://spatie.be/docs/laravel-medialibrary/v5/basic-usage/retrieving-media)
- [ Working with collections ](https://spatie.be/docs/laravel-medialibrary/v5/basic-usage/working-with-collections)

Converting images
-----------------

- [ Defining conversions ](https://spatie.be/docs/laravel-medialibrary/v5/converting-images/defining-conversions)
- [ Retrieving converted images ](https://spatie.be/docs/laravel-medialibrary/v5/converting-images/retrieving-converted-images)
- [ Regenerating images ](https://spatie.be/docs/laravel-medialibrary/v5/converting-images/regenerating-images)

Converting other file types
---------------------------

- [ Using image generators ](https://spatie.be/docs/laravel-medialibrary/v5/converting-other-file-types/using-image-generators)
- [ Creating a custom image generator ](https://spatie.be/docs/laravel-medialibrary/v5/converting-other-file-types/creating-a-custom-image-generator)

Advanced usage
--------------

- [ Working with multiple filesystems ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/working-with-multiple-filesystems)
- [ Adding custom properties ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/adding-custom-properties)
- [ Generating custom urls ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/generating-custom-urls)
- [ Storing media specific manipulations ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/storing-media-specific-manipulations)
- [ Using your own model ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/using-your-own-model)
- [ Using a custom directory structure ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/using-a-custom-directory-structure)
- [ Ordering media ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/ordering-media)
- [ Consuming events ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/consuming-events)
- [ Overriding default filesystem behavior ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/overriding-the-default-filesystem-behaviour)
- [ Soft deleting ](https://spatie.be/docs/laravel-medialibrary/v5/advanced-usage/soft-deletes)

API
---

- [ Adding files ](https://spatie.be/docs/laravel-medialibrary/v5/api/adding-files)
- [ Defining conversions ](https://spatie.be/docs/laravel-medialibrary/v5/api/defining-conversions)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-medialibrary                                                                                                                                                                                                                                    `

Working with collections
========================

If you have different types of files that you want to associate, you can put them in their own collection.

```
$newsItem = News::find(1);
$newsItem->addMedia($pathToImage)->toMediaCollection('images');
$newsItem->addMedia($pathToAnotherImage)->toMediaCollection('images');
$newsItem->addMedia($pathToPdfFile)->toMediaCollection('downloads');
$newsItem->addMedia($pathToAnExcelFile)->toMediaCollection('downloads');
```

All media in a specific collection can be retrieved like this:

```
// will return media instances for all files in the images collection
$newsItem->getMedia('images');

// will returns media instance for all files in the downloads collection
$newsItem->getMedia('downloads');
```

A collection can have any name you want. If you don't specify a name, the file will be added to a collection named `default`.

You can clear out a specific collection by passing the name to `clearMediaCollection`:

```
$newsItem->clearMediaCollection('images');
```
