Consuming events | laravel-medialibrary | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-medialibrary](https://spatie.be/docs/laravel-medialibrary/v3)  Advanced-usage  Consuming events

 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/v3/introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-medialibrary/v3/postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-medialibrary/v3/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-medialibrary/v3/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-medialibrary/v3/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-medialibrary/v3/changelog)

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

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

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

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

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

- [ Working with multiple filesystems ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/working-with-multiple-filesystems)
- [ Storing media specific manipulations ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/storing-media-specific-manipulations)
- [ Using your own model ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/using-your-own-model)
- [ Using a custom directory structure ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/using-a-custom-directory-structure)
- [ Consuming events ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/consuming-events)
- [ Generating custom urls ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/generating-custom-urls)
- [ Adding custom properties ](https://spatie.be/docs/laravel-medialibrary/v3/advanced-usage/adding-custom-properties)

API
---

- [ Adding files ](https://spatie.be/docs/laravel-medialibrary/v3/api/adding-files)
- [ Defining conversions ](https://spatie.be/docs/laravel-medialibrary/v3/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                                                                                                                                                                                                                                    `

Consuming events
================

###  On this page

1. [ Sample usage ](#content-sample-usage)

v3.10.1+

The medialibrary will fire the following events that your handlers can listen for:

### MediaHasBeenAdded

This event is fired after the a file has been saved to disk.

The event has a property `media` that holds the `\Spatie\MediaLibrary\Media`-object of which the file has been stored.

### ConversionHasBeenCompleted

This event is fired when a conversion has been completed.

The event has two public properties:

- `media`: the `\Spatie\MediaLibrary\Media`-object of which a conversion has been completed
- `conversion`: the conversion (an instance of `\Spatie\MediaLibrary\Conversion\Conversion`) that has just been completed

### CollectionHasBeenCleared

This event will be fired after a collection has been cleared.

The event has two public properties:

- `model`: the object that conforms to `\Spatie\MediaLibrary\HasMedia\Interfaces\HasMedia` of which a collection has just been cleared.
- `collectionName`: the name of the collection that has just been cleared

Sample usage
--------------------------------------------------------------------------------------------

First you must created a listener class. Here's one that will log the paths of added media.

```
namespace App\Listeners;

use Log;
use Spatie\MediaLibrary\Events\MediaHasBeenAdded;

class MediaLogger
{
    /**
     * @param \Spatie\MediaLibrary\Events\MediaHasBeenAdded $event
     */
    public function handle(MediaHasBeenAdded $event)
    {
        $media = $event->media;
        $path = $media->getPath();
        Log::info("file {$path} has been saved for media {$media->id}");
    }
}
```

Hook it up in `app/Providers/EventServiceProvider.php` to let Laravel know that your handler should be called when the event is fired:

```
protected $listen = [
    'Spatie\MediaLibrary\Events\MediaHasBeenAdded' => [
        'App\Listeners\MediaLogger'
    ],
];
```
