Search code examples
laravellaravel-7laravel-medialibrary

Laravel 7 : Interface 'Spatie\MediaLibrary\HasMedia' not found


I have this error while using the MediaLibrary package

Interface 'Spatie\MediaLibrary\HasMedia' not found
Trait 'Spatie\MediaLibrary\InteractsWithMedia' not found

My project details :-

php : 7.3.22

laravel : 7.28.3

I have tried follow the documentation v7 and also run the command below

composer require spatie/laravel-medialibrary

Other than that, I have also checked on other post which I have follow most of the steps but still getting back the same error

composer.json

"require": {
        "php": "^7.2.5",
        "fideloper/proxy": "^4.2",
        "fruitcake/laravel-cors": "^1.0",
        "guzzlehttp/guzzle": "^6.3",
        "laravel/framework": "^7.0",
        "laravel/tinker": "^2.0",
        "laravelcollective/html": "^6.2",
        "spatie/laravel-activitylog": "^3.16",
        "spatie/laravel-medialibrary": "^7.19"
    },

Model

use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Applicant extends Model implements HasMedia
{
    use LogsActivity, InteractsWithMedia;
}


Solution

  • Make sure to follow the documentation for the specific version of Laravel-medialibrary that you are using. In your case version 7. It looks like you were following the version 8 documentation.

    In version 7, the trait is Spatie\MediaLibrary\HasMedia\HasMediaTrait instead of Spatie\MediaLibrary\InteractsWithMedia. And the interface is Spatie\MediaLibrary\HasMedia\HasMedia instead of Spatie\MediaLibrary\HasMedia.

    If you want to use version 8 instead, change your requirements to "spatie/laravel-medialibrary": "^8.0" and then run composer update.