New in v6: How to use Spatie Media Library in Backpack

Hey there, awesome developers! We've got some super exciting news for you! Backpack v6 now comes with super cool, first-party support...

Karan Datwani
Karan Datwani

Hey there, awesome developers! We've got some super exciting news for you! Backpack v6 now comes with super cool, first-party support for one of the most loved Laravel packages out there – Spatie Media Library! 🎉

Spatie Media Library is a beloved choice for handling media in Laravel apps. Now, with Backpack's first-party support, it's even better! 😍

Requirements - Get Started with Spatie Media Library

1) Spatie's package

Make sure you have Spatie Media Library v10 installed in your project. If not, no worries! Just run this magical command and you're good to go:

composer require "spatie/laravel-medialibrary:^10.0.0"

Next up, prepare the database and run the migration:

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
php artisan migrate

Don't forget to create storage symbolic links for the default Laravel public disk. But hey, if you've already got it covered, high-five! 🖐️

php artisan storage:link

If you want to customize Spatie Media Library's configuration (you know, if you're the kind of developer who likes to add their own flavor), you can do that too! Simply publish the config file using:

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"

2) Spatie Media Library is on your Model

Before you start using Spatie Media Library in your Backpack v6 project, don't forget to prepare your model to make the magic happen! 😄 Don't Miss This Step!

In your YourModel.php file, make sure to implements HasMedia and use InteractsWithMedia to enable media management using Spatie Media Library:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class YourModel extends Model implements HasMedia // <--
    use InteractsWithMedia; // <--

So, now you're all set! Your model is ready to be powered by Spatie Media Library, and you can use Backpack's withMedia() magic to handle media like a champ! 🌟

Installing the Backpack Magic "medialibrary-uploaders"

All you need to do to integrate Spatie Media Library into your Backpack CRUD fields and columns is to require the medialibrary-uploaders using Composer:

composer require backpack/medialibrary-uploaders

Now, It's Ready to use in Your Fields & Columns🎉

Get your media uploads rolling with just one line of code. Use the withMedia() helper on your Backpack fields and columns definitions.

For Fields:


For Columns:


For Subfields (in a repeatable field, for example):

    ->label('Image Gallery')
            'name' => 'main_image',
            'label' => 'Main Image',
            'type' => 'image',
            'withMedia' => true,

More Fun - Customizing Your Way!

You know what's even cooler? You can customize the settings to match your needs! 😎

Customize Defaults:

    ->label('Main Image')
        'collection' => 'my_collection', // Customize media collection (default: Spatie's config default)
        'disk' => 'my_disk', // Customize disk (default: Spatie's config default)
        'mediaName' => 'custom_media_name', // Customize media name (default: the field name)

Customize Saving Process:

    ->label('Main Image')
        'whenSaving' => function ($spatieMedia, $backpackMediaObject) {
            return $spatieMedia->usingFileName('main_image.jpg')

Hold Up, There's More!

We've got your back with defining media collections in your model too! Check out Spatie's documentation and configure it in your model. Then simply refer to it in your Backpack field definition. Voilà! 😃

And Conversions? Easy Peasy!

// In Your Model.php
public function registerMediaConversions(): void

// In YourCrudController.php
    ->label('Main Image')
        'displayConversions' => 'thumb',

Sprinkle Some Custom Properties!

// In YourCrudController.php
    ->label('Main Image')
        'whenSaving' => function ($media) {
            return $media->withCustomProperties([
                'my_property' => 'value',
                'name' => 'i_cant_use_this_key',

With Backpack v6 and Spatie Media Library, you've got the dream package for media handling in your Laravel apps! Explore more on 🚀🌟

Want to receive more articles like this?

Subscribe to our "Article Digest". We'll send you a list of the new articles, every week, month or quarter - your choice.

Reactions & Comments

What do you think about this?

Latest Articles

Wondering what our community has been up to?