spiritix / lada-cache

A Redis based, fully automated and scalable database cache layer for Laravel
MIT License
538 stars 68 forks source link

Doesn't work with spatie / laravel-permission #99

Closed Belgiets closed 3 years ago

Belgiets commented 4 years ago

I am using default setup of spatie / laravel-permission package and lada-cache packages. Everything works well except user<->roles relation.

class User extends Authenticatable { use HasRoles; use LadaCacheTrait;

// ...

}

If i try to make request with 'roles' relation i'm getting an exception.

\App\User::whereHas('roles')->get();

TypeError Argument 1 passed to Spiritix\LadaCache\Reflector::getTablesFromWhere() must be an instance of Spiritix\LadaCache\Database\QueryBuilder, instance of Illuminate\Database\Query\Builder given, called in /home/belgiets/projects/laravel7/vendor/spiritix/lada-cache/src/Spiritix/LadaCache/Reflector.php on line 145

Reproduced on clear installation of:

denzndhauz commented 3 years ago

I'm having the same issue. I hope they can answer this.

packages versions:

"laravel/framework": "^7.0",
"spatie/laravel-permission": "^3.13",
"spiritix/lada-cache": "^5.0"
spiritix commented 3 years ago

Hi guys. As of now, there is no official support for this package. Will add it to the wish list, contributions are welcome.

Dietermartens commented 3 years ago

We have it working with these versions:

"laravel/framework": "^7.0",
"spatie/laravel-permission": "3.17.0",
"spiritix/lada-cache": "v5.1",
spiritix commented 3 years ago

Can you re-test this please with the latest release (v5.1.2) please? Could be that it fixes this issue as well.

zgetro commented 2 years ago

Main resone behind his error is only that Models/Role.php and Models/Permission.php both don't have use \Spiritix\LadaCache\Database\LadaCacheTrait; some how config/lada-cache.php exclude-tables feature is not working for spaties tables(did not checked how they ingnored).

if any one found same issue with other packages ( like spatie/permission ) they must need to check models has LadaCacheTrait trait.

in my case i have extended Permission, Role Models and add use Spiritix\LadaCache\Database\LadaCacheTrait;

<?php

namespace App;

use Spatie\Permission\Models\Permission as SpatiePermission;

class Permission extends SpatiePermission
{
    use \Spiritix\LadaCache\Database\LadaCacheTrait;
}
<?php

namespace App;

use Spatie\Permission\Models\Role as SpatieRole;

class Role extends SpatieRole
{
    use \Spiritix\LadaCache\Database\LadaCacheTrait;
}

to pass my custom models to spatie/permission package i have change configration config/permission.php

<?php

return [

    'models' => [

        'permission' => App\Permission::class,

        'role' => App\Role::class,

    ],

and It works Thanks @spiritix for wonderfull free code. hop that help to solve problem in upcomming version.

spiritix commented 2 years ago

@zgetro Thanks for providing a solution to this problem. I will reference it in the README, since similar workarounds can be used for other packages as well. Unfortunately, I don't think that issues with third-party libraries are generically solvable within Lada Cache. I'd have to look at every package in detail and develop a custom solution.