Home > Enterprise >  Is there any way to explode a table inside query?
Is there any way to explode a table inside query?

Time:12-25

I have this table in my database. » roles, and have content of 0,0,0,0,0,0

In this table i store user permissions. Example if he is an administrator it can be 0,0,0,0,0,1 or if he is an moderator and an administrator in the same time it can be 0,0,0,1,0,1.
But i don't know how i can show them in a query separated.
Example for administrator query only last parameter from that list 0,0,0,0,0,0.
So for it i have to do a explode in the query but i don't know how to do this.
I tried something like that to see if i can get the tables and explode it before query execute.

        $query = User::where(
    function($results) {
        dd($results);
        foreach($results as $result) {
            print($result);
        }
    })->first();

Someone have an idea how i can do this?

CodePudding user response:

if you have a fixed pattern you can "explode" it easily.

for example:

$result = "0,0,0,0,0,1"; // your data in for is string so:
$permission = explode(",",$result); //  convert to an array.

// outpult is:

print ($permission);

[0 , 0 , 0 , 0 , 0 , 1 ];

print ($permission[5]);

// output is:

1

now you can access each cell of the array.

but It's not a clear way to make permission and role. I suggest you use the different tables with different relationships.

CodePudding user response:

I think this is not convenient way of implementing roles and permissions. If you are using Laravel then there is already a way to implement permissions in the form of gates and policies. or you can implement it through a package like Spatie Package

CodePudding user response:

This might help it will return permissions ending with 1

   User::where(function ($builder){       
     $builder->where('permission', 'regex','%1');           
   });
           

CodePudding user response:

as a simple and appropriate solution, you can also write your, where condition as below

->whereRaw("SUBSTRING_INDEX(SUBSTRING_INDEX(permission_column_name, ',', 6), ',', -1) = 1")

as you can see in the above code, With SUBSTRING_INDEX(permission_column_name, ',', 6) function we will be able to extract everything up to the sixth value in your column separated by a comma.

Because SUBSTRING_INDEX() function also allows us to use negative values to extract fields counting from the right, we are using this ability to extract the rightmost field containing the last value of string with -1.

using the same above method you can also find any value available at any position as required in the given comma-separated string in a column.

CodePudding user response:

Assuming that each user has a role, you can add some methods to the User model to handle permissions.

Class User extends Authenticatable {
    private function getPermissions(): array {
        return explode(',', $this->role)
    }

    public function isAdministrator(): bool {
        $permissions = $this->getPermissions();

        return $permissions[5] == 1;
    }

    public function isModerator(): bool {
        $permissions = $this->getPermissions();

        return $permissions[3] == 1;
    }
}

Now you can use it in your code. Like this:

$user = auth()->user() // Or User::find($id) ...
if ($user->isAdministrator() || $user->isModerator()) {
        // Do something
}
  • Related