Skip to content
This repository has been archived by the owner on Apr 8, 2024. It is now read-only.

A Laravel Nova field for displaying and updating a Spatie Media Library model.

Notifications You must be signed in to change notification settings

jameslkingsley/nova-media-library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Nova Media Library

Attention: Please consider using this package instead, it has a lot more support for various medialibrary features, and will probably fit your needs better!

This package is designed to be used with the awesome media library package from Spatie. With this package you can add an image field for uploading single files to a resource, a generic file field for other types, and add an images field to resources to display all of their associated media.

use Kingsley\NovaMediaLibrary\Fields\Image;

Image::make('Avatar', 'avatar')
    ->usingConversion('thumb')
    ->preservingOriginal()

In this example we're defining a field called Avatar that uses the avatar collection. It's also calling usingConversion to set the thumb conversion as the image to be displayed (on detail and index). The other methods called are dynamically applied to the upload request - this lets you call any media-library method on the field..

If you want it to remove the old image before uploading the new one, be sure to make your model's media collection a single file collection.

When you want to upload a file that isn't an image, you can use the rudimentary File field included with this package. It follows the same format as registering an Image field.

use Kingsley\NovaMediaLibrary\Fields\File;

File::make('Invoice', 'invoice')

That's all there is to it! The rest of the configuration should be done in the media collection itself, such as:

public function registerMediaCollections()
{
    $this
        ->addMediaCollection('invoice')
        ->singleFile()
        ->acceptsFile(function (File $file) {
            return $file->mimeType === 'application/pdf';
        });
}

To show all media records for your resource simply add the Images field like so:

use Kingsley\NovaMediaLibrary\Fields\Images;

public function fields(Request $request)
{
    return [
        ...
        Images::make(),
    ];
}

This will automatically use the media attribute on your model (which the HasMediaTrait adds).

Note: You currently cannot create media directly from Nova.

Registering the media resource

If you'd like to use the media resource included with this package, you need to register it manually in your NovaServiceProvider in the boot method.

Nova::resources([
    \Kingsley\NovaMediaLibrary\Resources\Media::class
]);