spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.78k stars 1.07k forks source link

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) #1627

Closed fsaravi closed 4 years ago

fsaravi commented 5 years ago

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.

zsoro2 commented 4 years ago

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

public function registerMediaConversions() { }

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

acontia commented 4 years ago

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;

luke0x90 commented 4 years ago

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

Just add the void part to the end of the function

adellinocasas commented 4 years ago

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

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

cerw commented 3 years ago

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

valh1996 commented 3 years ago

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

cerw commented 3 years ago

@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.

nonso07 commented 3 years ago

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;

wow it worked for me like magic, but the issue I have now is that when I try posting the model, it display 'csrf token mismatched'

nonso07 commented 3 years ago

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;

wow it worked for me like magic, but the issue I have now is that when I try posting the model, it display 'csrf token mismatched'

Ok sorry I have seen the problem, it was because, the page session has expired, I refreshed the page and tried and it worked

gottaloveit commented 2 years ago

Hi, sorry, I know this issue is closed, but I ran across this too, when using a custom model extending the MediaCollection model. I fixed it by using registerAllMediaConversions() instead of registerMediaConversions().

custom media model App\Models\Media

<?php
namespace App\Models;

use App\Http\Traits\Uuids;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Media extends BaseMedia
{
    use HasFactory, Uuids;

    protected $attributes = [
        'access' => 'restricted',
        'status' => 'viewable',
        'publicApproved' => false,
        'publicReviewed' => false
    ];

}

User Model

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;

use App\Http\Traits\Uuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Haruncpi\LaravelUserActivity\Traits\Loggable;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

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

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function registerAllMediaConversions(): void
    {
        $this->addMediaConversion('thumb')
            ->width(50)
            ->height(50)
            ->sharpen(10);
    }
}

Hope this might help someone in the future if you get here by searching for this issue.

corbin1988 commented 1 year ago

Just adding to this: make sure to restart your server after changes. The fixes weren't being picked up until I restarted my server