ManojKiran
ManojKiran

Reputation: 6341

Laravel caching Database queries

i have created the project with user roles and permissions

Here is my Tables and Model

users--list of the application users --Model Name [User],

roles--list of the roles available inside the application --Model Name [Role],

permissions--list of the Permisisons available inside the application --Model Name [Permisions],

Here is my relationship tables

role_user Which hold the relationship between the roles table and users table

permission_role Which hold the relationship between the permissions table and roles table

permission_user Which hold the relationship between the permissions table and users table

My Relationship Code inside Model

User.php Model

/**
     * Many-to-Many relations with Role.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
    /**
     * Many-to-Many relations with Permission.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }

   public function hasPermission($permission)
    {
        return $this->hasPermissionThroughRole($permission) || (bool) $this->permissions->where('name',$permission->name)->count();
    }

    public function hasPermissionThroughRole($permission)
    {
        foreach($permission->roles as $role)
        {
            if($this->roles->contains($role))
            {
                return true;
            }
        }
        return false;
    }

    public function hasRoles($roles)
    {
        $roles = is_array($roles) ? $roles : func_get_args();
        foreach ($roles as $role) 
        {
            if ($this->hasRole($role)) 
            {
                return true;
            }
        }
        return false;
    }
    /**
     * Returns if the given user has an specific role.
     *
     * @param string $role
     *
     * @return bool
     */
    public function hasRole($role)
    {
        return $this->roles
            ->where('name', $role)
            ->first() != null;
    }

Role.php Model

/**
     * Many-to-Many relations with Permissions.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function permissions()
    {
        return $this->belongsToMany(Permission::class);
    }
    /**
     * Many-to-Many relations with Users.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }

Permission.php Model

/**
     * Belongs-to-Many relations with Role.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
    /**
     * Belongs-to-Many relations with User.
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
    /**
     * Belongs-to-Many relations with Modules.
     *
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
     */

And then finally i have create the ServiceProvider named as

PermissionServiceProvider

and Inside the boot method of the serviceprovider i have added the code

public function boot()
    {
        if (Schema::hasTable('permissions'))
        {         
             Permission::get()->map(function ($permission) 
             {
                 Gate::define($permission->name, function ($user) use ($permission) 
                {
                     return $user->hasPermission($permission);
                 });
             });


        }

        Blade::directive('role', function ($role)
            {
               return "<?php if(Auth::user()->hasRole({$role})): ?>";
            });
        Blade::directive('endrole', function ($role)
            {
                return "<?php endif; ?>";
            });
    }

Every functions and relationship is working fine but the Queries are running every time i hit the refresh button Running Queries

Is there any way to cache all the permisisons and roles to logged in user

Edited

As per Some Suggestions i have tried laravel cache package

It is not working for me

Upvotes: 1

Views: 8603

Answers (1)

IndianCoding
IndianCoding

Reputation: 2683

You are looking for laravel model caching pakage https://github.com/GeneaLabs/laravel-model-caching.

I recommend to install package with redis. Also it is very useful when working with queues.

Works as expected. Screenshots from my project.

Before:

enter image description here

After:

enter image description here

Upvotes: 3

Related Questions