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

Argument 2 passed to GeneaLabs\Laravel Model Caching\Cache Tags::__construct() must be an instance of Illuminate\Database\Eloquent\Model, string given #154

Closed
santosh401 opened this issue Aug 7, 2018 · 12 comments

Comments

@santosh401
Copy link

santosh401 commented Aug 7, 2018

Eloquent Query

 $product->in_stock = $request->value;
 $product->save();

Stack Trace
[2018-08-07 11:14:09] local.ERROR: Type error: Argument 2 passed to GeneaLabs\LaravelModelCaching\CacheTags::__construct() must be an instance of Illuminate\Database\Eloquent\Model, string given, called in /var/www/ddskart.devfbss.in/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php on line 85 {"userId":1,"email":"test@devfbss.in","exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Type error: Argument 2 passed to GeneaLabs\LaravelModelCaching\CacheTags::__construct() must be an instance of Illuminate\Database\Eloquent\Model, string given, called in /var/www/ddskart.devfbss.in/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php on line 85 at /var/www/ddskart.devfbss.in/vendor/genealabs/laravel-model-caching/src/CacheTags.php:17)
[stacktrace]
#0 /var/www/ddskart.devfbss.in/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php(85): GeneaLabs\LaravelModelCaching\CacheTags->__construct(Array, 'HP PRO M1136', Object(Illuminate\Database\Query\Builder))
#1 /var/www/ddskart.devfbss.in/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php(39): App\Product->makeCacheTags()
#2 /var/www/ddskart.devfbss.in/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php(157): App\Product->flushCache()
#3 /var/www/ddskart.devfbss.in/vendor/genealabs/laravel-model-caching/src/CachedBuilder.php(182): GeneaLabs\LaravelModelCaching\CachedBuilder->checkCooldownAndFlushAfterPersiting(Object(App\Product))
#4 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(628): GeneaLabs\LaravelModelCaching\CachedBuilder->update(Array)
#5 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(543): Illuminate\Database\Eloquent\Model->performUpdate(Object(GeneaLabs\LaravelModelCaching\CachedBuilder))
#6 /var/www/ddskart.devfbss.in/app/Http/Controllers/ProductController.php(190): Illuminate\Database\Eloquent\Model->save()
#7 [internal function]: App\Http\Controllers\ProductController->updateUpdate(Object(Illuminate\Http\Request), Object(App\Product))
#8 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#9 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\Routing\Controller->callAction('updateUpdate', Array)
#10 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\ProductController), 'updateUpdate')
#11 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\Routing\Route->runController()
#12 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Router.php(658): Illuminate\Routing\Route->run()
#13 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#14 /var/www/ddskart.devfbss.in/app/Http/Middleware/RedirectIfNotAdmin.php(24): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#15 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): App\Http\Middleware\RedirectIfNotAdmin->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#17 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#18 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#19 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#20 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#21 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'admin')
#22 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#23 /var/www/ddskart.devfbss.in/app/Http/Middleware/VerifyCsrfMiddleware.php(14): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#24 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): App\Http\Middleware\VerifyCsrfMiddleware->handle(Object(Illuminate\Http\Request), Object(Closure))
#25 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#26 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(67): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#27 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#28 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#29 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#30 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#32 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(63): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#33 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#34 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#35 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#36 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#37 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#38 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#39 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#40 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#41 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#42 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Router.php(660): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#43 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#44 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Router.php(601): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
#45 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Router.php(590): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#46 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#47 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#48 /var/www/ddskart.devfbss.in/vendor/fideloper/proxy/src/TrustProxies.php(56): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#49 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#50 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#52 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#53 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#54 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#55 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#56 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#57 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#58 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#59 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#60 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#61 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#62 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#63 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#64 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#65 /var/www/ddskart.devfbss.in/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#66 /var/www/ddskart.devfbss.in/public/index.php(55): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#67 {main}
"}

Environment

  • PHP: 7.2
  • OS: Ubuntu 18.04
  • Laravel: 5.5.*
  • Model Caching: 0.2.63

Using Cachable traits on other model works fine.

@mikebronner
Copy link
Owner

@santosh401 Please provide the eloquent query (what you provided is not the query, but the value assignment and save commands). We need the query that fetches the $product instance.

@santosh401
Copy link
Author

santosh401 commented Aug 8, 2018

$product is fetch with model binding

//route
Route::put('update/product/{product}', 'ProductController@updateStock');
//code
public function updateStock(Request $request, Product $product)
{
        // return $product; here I am getting $product as excpected
        $product->in_stock = $request->value;       
        $product->save();
}

@ysfkaya
Copy link

ysfkaya commented Aug 14, 2018

Is there any improvement in this bug ?

@santosh401
Copy link
Author

I am not facing any issue with the package on other model, all crud operation works well on other model.
Only insert and update is not working on product model

@mikebronner
Copy link
Owner

@ysfkaya Not yet, I haven't had any time to look at it yet.
@santosh401 Can you post your entire product model?

@santosh401
Copy link
Author

<?php

namespace App;

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;

class Product extends Model
{

    use
        Searchable,
        // Cachable,
        SoftDeletes;

    protected $fillable = ['sku','name','category','description','model','similar_products','base_price','sell_price','data','combo_qty','combo_discount','gst','price_without_gst'
    ];

    public function featuredImage()
    {
        return $this->hasOne(Media::class)->where('type', 'featured');
    }

    public function gallerImages()
    {
        return $this->hasMany(Media::class)->where('type', 'gallery');
    }

    public function questionAndAnswer()
    {
        return $this->hasMany(QuesAns::class)->orderBy('id', 'desc');
    }

    /**
     * Scope a query to only include in stock products.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeInStock($query)
    {
        return $query->where('in_stock', '>=', 0);
    }

    /**
     * Scope a query to newest.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeNewest($query)
    {
        return $query->orderBy('id', 'desc');
    }

    public function setModelAttribute($value)
    {
        $this->attributes['model'] = str_replace('#', '_', $value);
    }

    /**
     * The "booting" method of the model.
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('not_permanent_deleted', function (Builder $builder) {
            $builder->where('parmanent_deleted_at', null);
        });
    }

}

@ysfkaya
Copy link

ysfkaya commented Aug 18, 2018

I have solved this bug by override the makeCacheTags method .

use Cachable;

protected function makeCacheTags(): array
{
        $eagerLoad = $this->eagerLoad ?? [];
	$model = $this->model instanceof Model ? $this->model ? $this; // edited
	$query = $this->query instanceof Builder ? $this->query : app(Builder::class);

	$tags = (new CacheTags($eagerLoad, $model, $query))->make();

	return $tags;
}

I guess it triggered when the query attribute is not instanceof Builder class. Because sometimes the query attribute returns string in my model

@santosh401
Copy link
Author

I was getting this error
GeneaLabs\LaravelModelCaching\CacheTags::__construct() must be an instance of Illuminate\Database\Query\Builder, instance of Illuminate\Database\Eloquent\Builder given

and it was resolved with this modification

$tags = (new CacheTags($eagerLoad, $model, $query->getQuery()))->make();

Everything seems to be working.

@mwargan
Copy link

mwargan commented Sep 5, 2018

@santosh401 where did you put this? Could you give a bit more background code?

I tried the following:


namespace App\Models;

use GeneaLabs\LaravelModelCaching\CacheTags;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Search extends Model {
	use Cachable;
	protected function makeCacheTags(): array
	{
		$eagerLoad = $this->eagerLoad ?? [];
		$model = $this->model instanceof Model ? $this->model : $this; // edited
		$query = $this->query instanceof Builder ? $this->query : app(Builder::class);

		$tags = (new CacheTags($eagerLoad, $model, $query->getQuery()))->make();
		return $tags;
	}

Note I also changed $this->model instanceof Model ? $this->model ? $this; to $this->model instanceof Model ? $this->model : $this;.

Works fine but feels hack-y. Should I even be doing this like so?

@santosh401
Copy link
Author

I made new trait in App\Traits folder


namespace App\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use GeneaLabs\LaravelModelCaching\CacheTags;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;

trait CacheTrait
{
	use Cachable;

	protected function makeCacheTags(): array
	{
		$eagerLoad = $this->eagerLoad ?? [];
        $model = $this->model instanceof Model ? $this->model : $this; // edited
        $query = $this->query instanceof Builder ? $this->query : app(Builder::class);
        $tags = (new CacheTags($eagerLoad, $model, $query->getQuery()))->make();

        return $tags;
    }
}

and in my model

use App\Traits\CacheTrait;

class Product extends Model
{

    use CacheTrait;

this keeps my model clean
hope this helps

@mikebronner
Copy link
Owner

@santosh401 @mwargan @ysfkaya
I have applied the fix. However, I would like to be able to recreate the problem. Does anyone know why the model would be stored as a string in the Builder, instead of the object? I'm trying to figure out how to reproduce the issue, so that I can write tests for it.

@mwargan
Copy link

mwargan commented Oct 8, 2018

@mikebronner my code is open source at: https://github.com/mwargan/JustBookr

I'm not home now so i can't provide more info, but a quick search in the codebase will show you where I applied the "hack". Hopefully it'll help you reproduce this issue!

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

4 participants