0

I am using:

"php": "^8.0.2",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^9.19",
"laravel/sanctum": "^2.14.1",
"laravel/tinker": "^2.7",
"laravel/ui": "^3.4",
"laravelcollective/html": "^6.3",
"spatie/laravel-medialibrary": "^9.6.0",
"spatie/laravel-permission": "^5.5",
"toin0u/geocoder-laravel": "^4.5"

In my OptimisationJob Model I am trying to use:

use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia\HasMedia;

class OptimisationJob extends Model implements HasMedia {
   use HasMediaTrait;
}

I am getting the following error:

{
    "message": "Trait \"Spatie\\MediaLibrary\\HasMedia\\HasMediaTrait\" not found",
    "exception": "Symfony\\Component\\ErrorHandler\\Error\\FatalError",
    "file": "/var/www/ghgdrrtd/app/Models/OptimisationJob.php",
    "line": 15,
    "trace": []
}

Can anyone guide me please? I have gone through Spatie docs and also few more questions on stack but I am not able to get this fixed.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Yunus Aslam
  • 2,447
  • 4
  • 25
  • 39
  • Where in the documentation have you found to use `HasMediaTrait`? Model preparation on https://spatie.be/docs/laravel-medialibrary/v9/basic-usage/preparing-your-model looks a little different from yours – brombeer Aug 01 '22 at 12:09
  • @brombeer for example I have gone through these links [Here](https://laravel-news.com/uploading-avatar-images), [Also here](https://www.positronx.io/laravel-upload-images-with-spatie-media-library-tutorial/) – Yunus Aslam Aug 01 '22 at 12:11
  • I found an old page for version 7 that mentions `HasMediaTrait`: https://spatie.be/docs/laravel-medialibrary/v7/basic-usage/preparing-your-model Make sure to use the correct version 9 when searching the documentation. Those links probably use an old version – brombeer Aug 01 '22 at 12:12
  • I searched a lot but it seems that the way I have used is correct. But throws error while running. – Yunus Aslam Aug 01 '22 at 12:14
  • Hm, that second link you posted also uses version 9 and doesn't mention `HasMediaTrait` at all, not sure what you've been reading. The article in the first link is 4 years old, so most likely uses a different/older version than you – brombeer Aug 01 '22 at 12:15
  • No need to search a lot when there's (good) documentation. You use version 9, so follow the documentation for version 9: https://spatie.be/docs/laravel-medialibrary/v9/basic-usage/preparing-your-model – brombeer Aug 01 '22 at 12:16
  • Thank you so much! I will read it carefully and let you know if I figured it out. – Yunus Aslam Aug 01 '22 at 12:21
  • 1
    @brombeerThank you for your help. I was able to fix it. Actually I upgraded media library to the latest version where we do not need to specify traits. – Yunus Aslam Aug 01 '22 at 19:38

1 Answers1

0

I updated it to version "10.4.4". It works very well.

composer.json

"spatie/laravel-medialibrary": "10.4.4"
Ahsan Najam
  • 155
  • 1
  • 8