phayes0289's avatar

No Default Image When using Spatie Media Library?

I am using Spatie Media Library to allow users to upload an image of themselves that is shown throughout the project while they are logged in. Here is the code I have tried:

{{ URL::asset( auth()->user()->getFirstMediaUrl('profile') ??  '/images/user/Firefighter-Silhouette.png') }}

But it does not work. It simply displays nothing, but the asset help does put the domain in there

<img src="https://dev.fireops.local/" class="profile-image rounded-circle" >

Anybody have any ideas?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue seems to be with the code that retrieves the first media URL for the user's profile. Here's a possible solution:

{{ URL::asset(auth()->user()->getFirstMediaUrl('profile') ?: '/images/user/Firefighter-Silhouette.png') }}

In the code above, the ?? operator has been replaced with the ?: operator. The ?? operator checks if the left-hand side expression is null or empty, and if so, it returns the right-hand side expression. However, in this case, the getFirstMediaUrl method might be returning an empty string instead of null, which is why the default image is not being displayed.

By using the ?: operator, if the left-hand side expression is null, empty, or evaluates to false, it will return the right-hand side expression.

Make sure that the path to the default image is correct. If the image is located in the public/images/user/ directory, the path should be relative to the public directory.

If the issue persists, ensure that the default image file exists in the specified location and that the file name is correct.

1 like

Please or to participate in this conversation.