Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Nova + Spatie Media library

Trying to use Laravel Nova with Spatie Media Library. I created upload field like this:

Image::make('Logo')
        ->store(function (Request $request, $model) {
            $model->addMediaFromRequest('logo')->toMediaCollection('manufacturers');
        }),

Seams ok, but Nova still trying to save file name to "logo" column in manufacturers table.

Original excample to customize this field was:

File::make('Attachment')
    ->store(function (Request $request, $model) {
        return [
            'attachment' => $request->attachment->store('/', 's3'),
            'attachment_name' => $request->attachment->getClientOriginalName(),
            'attachment_size' => $request->attachment->getSize(),
        ];
    })
like image 235
RomkaLTU Avatar asked Oct 27 '25 17:10

RomkaLTU


1 Answers

I found a work around by setting an empty mutator on the model. In your case it would be:

class Manufacturer extends Model implements HasMedia
{
    use HasMediaTrait;

    public function setLogoAttribute() {}

    //...
}

Here's an example of my entire implementation. Note that currently with Nova 1.0.6, the preview() method is not working, it's returning the thumbnail() url.

App/GalleryItem

class GalleryItem extends Model implements HasMedia
{
    use HasMediaTrait;

    public function setImageAttribute() {}

    public function registerMediaConversions(Media $media = null)
    {
        $this->addMediaConversion('thumbnail')
            ->fit(Manipulations::FIT_CROP, 64, 64);

        $this->addMediaConversion('preview')
            ->fit(Manipulations::FIT_CROP, 636, 424);

        $this->addMediaConversion('large')
            ->fit(Manipulations::FIT_CONTAIN, 1920, 1080)
            ->withResponsiveImages();
    }

    public function registerMediaCollections()
    {
        $this->addMediaCollection('images')->singleFile();
    }
}

App/Nova/GalleryItem

class GalleryItem extends Resource
{
    public static $model = 'App\GalleryItem';

    public static $with = ['media'];

    public function fields(Request $request)
    {
        return [
            Image::make('Image')
                ->store(function (Request $request, $model) {
                    $model->addMediaFromRequest('image')->toMediaCollection('images');
                })
                ->preview(function () {
                    return $this->getFirstMediaUrl('images', 'preview');
                })
                ->thumbnail(function () {
                    return $this->getFirstMediaUrl('images', 'thumbnail');
                })
                ->deletable(false);
        ];
    }
}
like image 64
Staysee Avatar answered Oct 29 '25 07:10

Staysee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!