Skip to content
Discussion options

You must be logged in to vote

@andrezanna - I didn't want to have to generate a preview to use it so this may help if you didn't want to create one either.

You can create a custom class for your Media and modify the previewUrl function to default to the regular URL if there is no preview:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;

class Media extends BaseMedia
{
    protected function previewUrl(): Attribute
    {
        return Attribute::get(
            fn () => $this->hasGeneratedConversion('preview') ? $this->getUrl('preview') : $this->getUrl(),
        );
    }
}

make sure to update the media-library.php co…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by andrezanna
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants