PHP code example of backpack / medialibrary-uploaders

1. Go to this page and download the library: Download backpack/medialibrary-uploaders library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

backpack / medialibrary-uploaders example snippets


CRUD::field('avatar')->type('image')->withMedia();

// you can also do that on columns:
CRUD::column('avatar')->type('image')->withMedia();

// and on subfields:
CRUD::field('gallery')
        ->label('Image Gallery')
        ->type('repeatable')
        ->subfields([
            [
                'name' => 'main_image',
                'label' => 'Main Image',
                'type' => 'image',
                'withMedia' => true,
            ],
        ]); 

CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'collection' => 'my_collection', // default: the spatie config default
            'disk' => 'my_disk', // default: the spatie config default
            'mediaName' => 'custom_media_name' // default: the field name
        ]);

CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'whenSaving' => function($spatieMedia, $backpackMediaObject) {
                return $spatieMedia->usingFileName('main_image.jpg')
                                    ->withResponsiveImages();
            }
        ]);

// In your Model.php

public function registerMediaCollections(): void
{
    $this
        ->addMediaCollection('product_images')
        ->useDisk('products');
}

// And in YourCrudController.php
CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'collection' => 'product_images', // will pick the collection definition from your model
        ]);

// In your Model.php

public function registerMediaConversions(): void
{
    $this->addMediaConversion('thumb')
                ->width(368)
                ->height(232)
                ->keepOriginalImageFormat()
                ->nonQueued();
}

// And in YourCrudController.php
CRUD::field('main_image')
        ->label('Main Image')
        ->type('image')
        ->withMedia([
            'displayConversions' => 'thumb'
        ]);
        
// you can also configure aditional manipulations in the `whenSaving` callback
->withMedia([
    'displayConversions' => 'thumb',
    'whenSaving' => function($media) {
        return $media->withManipulations([
            'thumb' => ['orientation' => 90]
        ]);
    }
]);


'whenSaving' => function($media) {
        return $media->withCustomProperties([
            'my_property' => 'value',
            'name' => 'i_cant_use_this_key'
        ]);
    }

// the saved custom properties will be: 
//  - [my_property => value, name => main_image, repeatableRow => null, repeatableContainerName => null]`