0

I have store information in MySQL table with PHP (Laravel) encrypted string, now how I can sort it using MySQL query in Laravel.

ex. The first name is store in the database with PHP encrypted string now I do sorting the first name from the database when getting the number of records.

2
  • 3
    Welcome to StackOverflow! This is not a free coding service. You are expected to make your best attempt at solving the problem, and then sharing your code so that we may help you. Please edit your question to include your code. Also see How do I ask a good question?. Commented Oct 4, 2021 at 5:10
  • 2
    Is the encryption algorithm present for MySQL implementation as well? If yes, then you can also decrypt and sort before getting from the DB. Commented Oct 4, 2021 at 5:41

1 Answer 1

1

Use eloquent's accessor feature.

Example:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Crypt;

class User extends Model
{
    /**
     * Get the user's first name.
     *
     * @param  string  $value
     * @return string
     */
    public function getFirstNameAttribute($value)
    {
        return Crypt::decryptString($value);
    }
}

Then you can easily sort using collection's sorting.

Example:

User::all()->sortBy('first_name');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.