Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting error while update item #202

Closed
anilgangani04 opened this issue Jan 17, 2019 · 6 comments
Closed

Getting error while update item #202

anilgangani04 opened this issue Jan 17, 2019 · 6 comments
Assignees

Comments

@anilgangani04
Copy link

anilgangani04 commented Jan 17, 2019

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

  • PHP: [7.1.26]
  • OS: [Ubuntu 18.04]
  • Laravel: [Laravel Framework Lumen (5.6.4) (Laravel Components 5.6.*)]
  • Model Caching: [0.3.4]

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

@mikebronner
Copy link
Owner

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

@mycarrysun
Copy link

remove the class Builder from the line that says:

static::addGlobalScope('order', function (Builder $builder) {

@mikebronner
Copy link
Owner

mikebronner commented Mar 28, 2019

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

@mycarrysun
Copy link

@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
Copy link
Author

Thanks guys

@mikebronner
Copy link
Owner

mikebronner commented Mar 29, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants