mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.27k stars 218 forks source link

Doesn't work with Spatie's Laravel QueryBuilder #275

Open backstageel opened 5 years ago

backstageel commented 5 years ago

Describe the bug Seems Like this Package doesn't work when using Spatie's Laravel query builder to fetch the data (https://github.com/spatie/laravel-query-builder). I've checked that package and they don't override newEloquentBuilder() method, so all should work.

By not working i mean no cache data is returned and the query still hits the database.

Eloquent Query This query don't work

$student = QueryBuilder::for(Student::class)
            ->join('entidades', 'entidades.id', '=', 'alunos.entidade_id')
            ->where('entidades.user_id', $user->id)
            ->first();

But this work:

$student = Student::join('entidades', 'entidades.id', '=', 'alunos.entidade_id')
            ->where('entidades.user_id', $user->id)
            ->first();

Stack Trace No stack trace, because no error is generated. Just no data is cached

Environment

mikebronner commented 5 years ago

@backstageel Thanks for submitting your issue. This package will not work with third-party query builders, only with Laravel's query builder. Sorry.

backstageel commented 5 years ago

Actually, i got this Working...

It was as simple as creating a custom builder class that extends Spatie's QueryBuilder and apply the traits from this package on it:

namespace App\Builders;

use GeneaLabs\LaravelModelCaching\Traits\Buildable;
use GeneaLabs\LaravelModelCaching\Traits\BuilderCaching;
use GeneaLabs\LaravelModelCaching\Traits\Caching;
use Spatie\QueryBuilder\QueryBuilder;

class CustomBuilder extends  QueryBuilder
{
    use Buildable;
    use BuilderCaching;
    use Caching;
}
mikebronner commented 5 years ago

I'll add this to the documentation, thanks!

francoism90 commented 3 years ago

@mikebronner This doesn't seem to be added to the documentation? :)

@backstageel Does it still work for you?

mikebronner commented 3 years ago

I think I removed it again, because I started work on developing an internal solution that wouldn't require more work. Unfortunately I have not been able to complete that at this time due to life circumstances changing A LOT this year.

seek-x2y commented 3 years ago

"a custom builder class" not work for me

"message": "Property [localMacros] does not exist on the Eloquent builder instance.", "exception": "Exception"

PitchRE commented 2 years ago

any update?