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

orderByEncrypted functionality #20

Open
luckyhanna opened this issue May 31, 2022 · 0 comments · May be fixed by #21
Open

orderByEncrypted functionality #20

luckyhanna opened this issue May 31, 2022 · 0 comments · May be fixed by #21

Comments

@luckyhanna
Copy link

Besides the where clauses, and orderBy would also be useful. In order to be able to sort the data by encrypted fields, I extended the EloquentBuilder class and added the following code:

class CustomEloquentBuilder extends EncryptionEloquentBuilder
{
    public function orderByEncrypted($column, $direction = 'asc') {
        $salt = substr(hash('sha256', env('APP_KEY')), 0, 16);
        return self::orderByRaw("CONVERT(AES_DECRYPT(FROM_bASE64(`{$column}`), '{$salt}') USING utf8mb4) {$direction}");
    }
}
trait CustomEncryptedAttribute
{
    use EncryptedAttribute;

    public function newEloquentBuilder($query)
    {
        return new CustomEloquentBuilder($query);
    }
}

It would be great to have it as a built-it function in the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant