mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.23k stars 212 forks source link

Could not check compatibility between Cachable::newBelongsToMany and Model::newBelongsToMany #381

Closed pokemonhan closed 3 years ago

pokemonhan commented 3 years ago

Describe the bug I got the below error when I installed on laravel 8.

Warning: Could not check compatibility between GeneaLabs\LaravelModelCaching\Traits\Cachable::newBelongsToMany(GeneaLabs\LaravelModelCaching\EloquentBuilder $query, Illuminate\Database\Eloquent\Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = NULL) and Illuminate\Database\Eloquent\Model::newBelongsToMany(Illuminate\Database\Eloquent\Builder $query, Illuminate\Database\Eloquent\Model $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName = NULL), because class GeneaLabs\LaravelModelCaching\EloquentBuilder is not available in /var/www/jianghu_entertain/app/Models/BaseModel.php on line 110

image image

Environment

Additional context image image

danniehansen commented 3 years ago

@harrisdt15f

Not sure if this fixes it for you - but i managed to get it working by adding the Service provider class added by genealabs in my unit test getPackageProviders method. Error appeared due to a class_alias not getting loaded before unit test ran.

protected function getPackageProviders($app)
    {
        return [OtherServiceProvider::class, \GeneaLabs\LaravelModelCaching\Providers\Service::class];
    }
mikebronner commented 3 years ago

@harrisdt15f Unfortunately I am unable to replicate this issue. If you could submit a failing unit test, that would help immensely! :)

dmason30 commented 3 years ago

@mikebronner Its a PHP Warning so its not breaking the code but its still an inconvenient message to get. I am only getting this when running unit tests in my Laravel 8 application. It is specifically complaining about type hinting the $query argument as the LaravelModelCaching\EloquentBuilder instead of the Eloquent\Builder.

PHP Warning:  Declaration of GeneaLabs\LaravelModelCaching\Traits\Cachable::newBelongsToMany(
GeneaLabs\LaravelModelCaching\EloquentBuilder $query,
Illuminate\Database\Eloquent\Model $parent, 
$table, 
$foreignPivotKey,
$relatedPivotKey, 
$parentKey, 
$relatedKey,
$relationName = NULL) should be compatible with Illuminate\Database\Eloquent\Model::newBelongsToMany(
Illuminate\Database\Eloquent\Builder $query, 
Illuminate\Database\Eloquent\Model $parent, 
$table, 
$foreignPivotKey,
$relatedPivotKey, 
$parentKey, 
$relatedKey, 
$relationName = NULL) in /Users/xxxx/PhpstormProjects/TMC-BACKEND/site/app/Models/CurrencyModelsBestTrackerSetting.php 
on line 45

Locally I changed the $query argument locally to match Model::newBelongsToMany in ModelCaching::newBelongsToMany and the message goes away.

roelVerdonschot commented 3 years ago

@dmason30 Your warning looks different. I get the same error as @harrisdt15f and it stops my application from working. It currently prevents us from upgrading to Laravel 8.

dmason30 commented 3 years ago

@roelVerdonschot Ok so I was running on PHP 7.3 for the above issue. It appears that the message has changed between PHP 7.3 and 7.4.

I have setup an example repo (see link below) and if you click on the "Executing PHP Tests" section on both the PHP 7.3 and PHP 7.4 runs and you can see it outputs both variations of the message but on PHP 7.4 it is not stopping script execution. Could this be down to PHP configuration?

https://github.com/dmason30/model-caching-example/runs/1359149642?check_suite_focus=true

@mikebronner I hope this serves as proof that this issue exists and can be replicated. I will do a PR and run the fix in my example repo to show that these messages no longer occur.

dmason30 commented 3 years ago

I have created a PR here with before and after comparisons using the example repo mentioned above.

https://github.com/GeneaLabs/laravel-model-caching/pull/382