New in v6: How to use Uploaders - Making Upload Fields Easier Than Ever!

Hey there, my friends! I've got some exciting news about Backpack v6 that will make your life as a developer even easier. Introducing a...

Karan Datwani
Karan Datwani
Share:

Hey there, my friends! I've got some exciting news about Backpack v6 that will make your life as a developer even easier. Introducing an amazing new feature called "Uploaders," and trust me, it's a game-changer for handling file uploads! 🌟

Say Goodbye to Complex File Handling

Before v6, handling file uploads involved creating mutators or custom code. But with Uploaders, Backpack has taken another leap forward! Now, you can fully set up your upload fields using purpose-built classes called Uploaders. These Uploaders handle all the upload and retrieval logic for you. Say goodbye to manual validation and custom code - Backpack's got your back! 😎

How It Works - Super Simple!

Adding an upload field to your operation is now a breeze! You just need to tell Backpack to use the appropriate Uploader for your field using the withFiles() method:

CRUD::field('document')->type('upload')->withFiles();

Can you believe it? That's all you need! 🀯 Backpack will handle everything from the upload to the storage and even deletion of files for you. No more complex setups - it's that easy! Plus, we've provided a bunch of configurations for common use cases. And guess what? You can even create your own custom Uploader class if needed. How cool is that? Check out more about Uploader classes in the documentation!

Configuring Uploaders - Customize Your Uploads

You have complete control over your uploads! If you want to customize the upload options, simply pass an array of options to the withFiles() method:

CRUD::field('avatar')
    ->type('upload')
    ->withFiles([
        'disk' => 'public', // Set the disk where files will be stored
        'path' => 'uploads', // Set the path inside the disk for file storage
        // Add more options like deleteWhenEntryIsDeleted, temporaryUrl, and more!
    ]);

Naming Uploaded Files

Backpack has a smart naming strategy for uploaded files. For different field types, it generates unique names to avoid collisions. But hey, you can customize that too! Just create a class implementing FileNameGeneratorInterface and pass it to the upload configuration:

CRUD::field('avatar')->type('upload')->withFiles([
    'fileNamer' => \Backpack\CRUD\app\Library\Uploaders\Support\FileNameGenerator::class,
]);

You can even pass a closure if you prefer. The choice is yours!

Using Uploaders in Subfields

Oh, and here's more good news! You can use Uploaders in subfields too. The configuration remains the same, and it's just as easy:

[
    [
        'name' => 'avatar',
        'type' => 'upload',
        'withFiles' => true,
    ],
    [
        'name' => 'attachments',
        'type' => 'upload_multiple',
        'withFiles' => [
            'path' => 'attachments',
        ],
    ],
]

Configure Uploaded Files Deletion

If you want Backpack to automatically delete uploaded files when an entry is deleted, simply set up the fields in the DeleteOperation too:

protected function setupDeleteOperation()
{
    CRUD::field('photo')->type('upload')->withFiles();
    // Your other field configurations here
}

That's it, my friends! With Uploaders in Backpack v6, managing file uploads has become a breeze. It's super easy, highly customizable, and will save you loads of time! Check out more details in the official documentation. Happy coding, and enjoy the magic of Uploaders! πŸš€πŸ”₯

Find more info on Uploaders Docs.

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?