mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 217 forks source link

Getting error while update item #202

Closed anilgangani04 closed 5 years ago

anilgangani04 commented 5 years ago

Describe the bug I am getting error as below while update Item object...

[2019-01-17 08:34:58] dev.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to App\Models\App\Item::App\Models\App{closure}() must be an instance of App\Models\App\Builder, instance of GeneaLabs\LaravelModelCaching\CachedBuilder given, called in /home/anilg/data1/html//vendor/illuminate/database/Eloquent/Builder.php on line 921 in /home/anilg/data1/html//app/Models/App/Item.php:139 Stack trace:

0 /home/anilg/data1/html/****/vendor/illuminate/database/Eloquent/Builder.php(921): App\Models\App\Item::App\Models\App{closure}(Object(GeneaLabs\LaravelModelCaching\CachedBuilder))

Item Model has below additional code :

protected static function boot() { parent::boot();

    # Order by order DESC
    static::created(function() {
        Artisan::call('cache:clear');
    });

    static::updated(function() {
        Artisan::call('cache:clear');
    });

    static::deleted(function() {
        Artisan::call('cache:clear');
    });

    static::addGlobalScope('order', function (Builder $builder) {
        $builder->orderBy('order', 'asc');
    });
}

Environment

Additional context If I clear cache by command "php artisan cache clear" then It's working well for only first time.

mikebronner commented 5 years ago

Thanks for posting this. Can you provide the full stack trace of the error? That will help me identify what is going on.

mycarrysun commented 5 years ago

remove the class Builder from the line that says:

static::addGlobalScope('order', function (Builder $builder) {
mikebronner commented 5 years ago

Thanks @mycarrysun :). This will take some thinking. I see that the global scope method is passing in the closure. Grrr.

mycarrysun commented 5 years ago

@mikebronner if he wants to keep the class, he can just use:

// App\Models\App\Item.php
protected static function boot()
{
    parent::boot();

    # Order by order DESC
    static::created(function() {
        Artisan::call('cache:clear');
    });

    static::updated(function() {
        Artisan::call('cache:clear');
    });

    static::deleted(function() {
        Artisan::call('cache:clear');
    });

    static::addGlobalScope('order', function (Illuminate\Database\Eloquent\Builder $builder) {
        $builder->orderBy('order', 'asc');
    });
}

The error will go away. This is not a bug with the library.

anilgangani04 commented 5 years ago

Thanks guys

mikebronner commented 5 years ago

@mycarrysun I will research your solution to see if it solves the global scope problems #106 Thanks for your help troubleshooting :)