laravel spatie media library does not recognise the model

57 Views Asked by At
$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?

1

There are 1 best solutions below

0
marcin2137 On BEST ANSWER

try to use

$path = $user->hasMedia('*');

to get url to file

$path = $user->getFirstMediaUrl('*');

and if you want to have url to thumb file

$path = $user->getFirstMediaUrl('*', 'thumb');