Blog Detail Pic

Set Default In Spatie Media Library

Avatar
by Kwilio | Apr 15, 2021

There are many cases where you would need to set a default fall back image in your Spatie media library url. An example is user avatars or client photos. This tutorial will teach you how to do that.

Steps

1. In your module at the top add the following class

use Spatie\MediaLibrary\MediaCollections\File;

2. Add the following function to your Model

public function registerMediaCollections(): void
    {
        $this->addMediaCollection('products')
            ->useFallbackUrl('/images/product-default.jpg')
            ->useFallbackPath(public_path('/images/product-default.jpg'));
    }

Thats it!