Basic usage

In these basic examples we'll use the BlogPost model to add, update and remove some media files.

Use the list command to see all all available commands for these examples:

php artisan list blogpost

Blogpost commands

We've already prepared the BlogPost model for you by implementing HasMedia and the HasMediaTrait. Read more about preparing your model in the documentation.

Adding media to the blogpost (BlogPostAddMedia.php)

Use the following command to add media file to the existing BlogPost model. Media files aren't limited to images. Try adding demofiles/hamlet.pdf or demofiles/coolvideo.webm. You can add as many media files as you want.

php artisan blogpost:add-media demofiles/sheep.jpg

Blogpost add media

As you can see, the media file is automatically copied to the configured storage disk. This is because we used the preservingOriginal method. If you want to move the media file to the disk instead of copying it you can ignore this method.

Read more about adding media files to models in the documentation.

Fetching all associated media from the blogpost (BlogPostListMedia.php)

Use the following command to fetch all media associated with the blogpost:

php artisan blogpost:list-media

Blogpost list media

As you can see we've automagically added some meta data for your media files such as mime_type, size and human_readable_size.

Updating associated media (BlogPostUpdateMedia.php)

As you can see from the previous example, every media file received a name and filename. You can easily update these names the way you're used to in Eloquent models.

php artisan blogpost:update-media {id} --name="new name" --filename="new-filename.jpg"

Blogpost list media

Delete media files (BlogPostDeleteMedia.php)

The medialibrary provides a delete method to delete a single media association. This method will also remove the file from your filesystem.

php artisan blogpost:delete-media {id}

Blogpost list media

As you can see, the directory in storage/app/media with the Media's id has been removed as well.

To delete all media files associated with a model you can use the clearMediaCollection method.

php artisan blogpost:delete-media

Blogpost list media

As you can see, all of the media directories associated with the BlogPost have now been removed from storage/app/media directory.