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
]);
// 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]
]);
}
]);