Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

65-99's avatar
Level 1

laravel spatie media library does not recognise the model

$user = User::find(54); $path = $user->hasMedia(); dd($path);

respond: false // app\Http\Controllers\User\UserController.php:24

record in db:

INSERT INTO media (id, model_type, model_id, uuid, collection_name, name, file_name, mime_type, disk, conversions_disk, size, manipulations, custom_properties, generated_conversions, responsive_images, order_column, created_at, updated_at) VALUES (27, 'App\Models\User', 54, '/uuid/', 'avatars', 'bmw2', 'bmw2.png', 'image/png', 'public', 'public', 2422290, '[]', '[]', '{"thumb":true}', '[]', 1, '2024-01-21 13:34:40', '2024-01-21 13:34:42');

user.php (model)

use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\MediaCollections\Models\Media; use Spatie\MediaLibrary\HasMedia; use Spatie\MediaLibrary\InteractsWithMedia;

class User extends Authenticatable implements HasMedia {     use HasApiTokens, HasFactory, Notifiable;     use InteractsWithMedia;

    public function registerMediaConversions(Media $media = null): void     {         $this->addMediaConversion('thumb')               ->width(200)               ->height(200);     }

anyone have an idea why I don't get a response from the database that a user has a photo assigned to them?

0 likes
3 replies
tisuchi's avatar

@65-99 If you set correctly the media library, can you try this?

php artisan cache:clear
enoch91's avatar

@65-99 use this $user->hasMedia('avatars') if the issue persists, try to print out the results of $user->getMedia()

65-99's avatar
Level 1

$user->getFirstMediaUrl('*', 'avatar');

this worked thanks for help

Please or to participate in this conversation.