Laravel-medialibrary: Declaration of App\Models\Task::registerMediaConversions(?App\Models\Media $media = NULL) must be compatible with Spatie\MediaLibrary\HasMedia\HasMedia::registerMediaConversions(?Spatie\MediaLibrary\Models\Media $media = NULL)

Created on 11 Nov 2019  路  7Comments  路  Source: spatie/laravel-medialibrary

I set my Media Model in config file that is extend of Spatie\MediaLibrary\Models\Media and it is in App\Models namespace.

when I try to add registerMediaConversions method to my model that has some media, raise this error:

Declaration of App\Models\Task::registerMediaConversions(?App\Models\Media $media = NULL) must be compatible with Spatie\MediaLibrary\HasMedia\HasMedia::registerMediaConversions(?Spatie\MediaLibrary\Models\Media $media = NULL)

this error is completely clear. the method should completely compatible with method that declares in interface HasMedia.

Most helpful comment

public function registerMediaConversions(Media $media = null) : void

Just add the void part to the end of the function

All 7 comments

This is worked for me. (php7.3, laravel6.4)

  • from this in my Model:

public function registerMediaConversions() { }

  • to this in my Model:

public function registerMediaConversions(Media $media = null) { }

In case someone else has this error... don't forget to add this at the beginning of your model:
use Spatie\MediaLibrary\MediaCollections\Models\Media;

public function registerMediaConversions(Media $media = null) : void

Just add the void part to the end of the function

worked for me @priceluke, thanks
otherwise I would get:

_Method 'App\Channel::registerMediaConversions()' is not compatible with method 'Spatie\MediaLibrary\HasMedia::registerMediaConversions()'._

But what if I use a custom Media model? I still get this error..

@cerw hi, do you find a solution at this problem please?

@valh1996 not really I had to use the Media that comes from the Package. So far no real problem but you do all the custom relationship method from the custom model.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stayallive picture stayallive  路  4Comments

Nks picture Nks  路  3Comments

brendt picture brendt  路  4Comments

intrepidws picture intrepidws  路  3Comments

mokhosh picture mokhosh  路  3Comments