Spatie\MediaLibrary

You're browsing the documentation for an old version of MoonShine. Consider upgrading your project to MoonShine 2.x.

Extends Image * has the same features

This field belongs to a separate package, you have to complete the installation before using it

composer require visual-ideas/moonshine-spatie-medialibrary

The field is purposed for work with the Laravel-medialibrary package made by Spatie

Before using the Spatie\MediaLibrary field, make sure that:

  • The spatie/laravel-medialibrary package is installed and configured
  • The visual-ideas/moonshine-spatie-medialibrary package is installed
  • The field passed to Spatie\MediaLibrary is added as the name of the collection via ->addMediaCollection('Field')

In the model:

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
 
class ModelClass extends Model implements HasMedia
//...
use InteractsWithMedia;
 
//...
public function registerMediaCollections(): void
{
$this->addMediaCollection('cover');
}
//...

In the MoonShine-resource:

use VI\MoonShineSpatieMediaLibrary\Fields\MediaLibrary;
//...
MediaLibrary::make('Cover', 'cover'),
//...

By default, the field works in a single image mode

use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
 
class ModelClass extends Model implements HasMedia
//...
use InteractsWithMedia;
//...
public function registerMediaCollections(): void
{
$this->addMediaCollection('cover')->singleFile();
}
//...

If you want to use a field to load multiple images, add the ->multiple() method when declaring the field

//...
MediaLibrary::make('Gallery', 'gallery')->multiple(),
//...