Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Encrypt and Decrypt Id in Laravel

Encrypt and Decrypt Id in Laravel

In this tutorial, I will give you a simple example of encode and decode in laravel, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application, Sometimes we need to pass ids as parameters in URL to use that value on the blade file or controller. But passing ids or other parameters directly is not safe because it can cause security issues.

Importance of Encryption

Encryption is a way of scrambling data so that only allowed parties can understand the information. This is helpful for transferring sensitive data because there are some chances for a spammer or hacker to target the information.

I will give you a very simple example of encrypt and decrypt Id in url in laravel, using this you can encrypt and decrypt any type of parameters in url.

Recomendend Article : Generate unique Order numbers in Laravel

To avoid such Security issues we will encrypt id in url in laravel application.


At first, we define Url Route in web.php file, we pass Id in the Url and Create a function in our Controller.


Blade File

We have a Blade file name : list.blade.php, where we have some data, now we want to edit our products, and we need to be pass Id in Url and get Id from Controller.

We Simply use Laravel default c, using this you can encrypt and decrypt any type parameters in Url.

Before Encryption

Encrypt and Decrypt Id in Laravel

See the above image, where you can see Id’s without encryption.

 <table class="table">
                  <th>Order / Product No.</th>
                  <th>Product Name</th>
                  <th>Product Description</th>
               @foreach($productList as $key => $data)
                  <td>{{ $key+1 }}</td>
                  <td><a href="{{route('view.product',[$data['id'],$data['product_no']])}}">View</a>

                  @php $prodID= Crypt::encrypt($data->id); @endphp
                  <!--Encrypt ID and store as $prodID-->
                  <a href="{{route('edit.product',$prodID)}}">Edit</a>

After Encryption

Encrypt and Decrypt Id in Laravel

Now using laravel default encrypt, you can see the Id’s is now encrpt and it is hard to read.


We have a ProductController, where we have a editProduct function, now we decode our Ids from Url using decrypt. now you are able to get all data according to Id’s.


  namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\Crypt;
  use App\Product;
  class ProductController extends Controller
   public function editProduct($id){
        $prodID = Crypt::decrypt($id);
        $Data   = Product::find($prodID);
        return view('product.view',compact('Data'));

Now you can see the output, we clicked on Id 4 and encrypt this on Blade file, After using decode the Id from our controller.
In the below Image see the encrypted URL, Now we decrypted Id, and we are able to Get Data.

Encrypt and Decrypt Id in Laravel

Final Words

In this article, we learned “How to Encrypt and Decrypt Id in Laravel”, I hope this article will help you with your Laravel application Project.

Also Read : Store Multiple Select Value In Laravel.