Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
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.
Route::get('/edit-product/{id}','ProductController@editProduct')->name('edit.product');
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.
See the above image, where you can see Id’s without encryption.
<table class="table">
<thead>
<tr>
<th>S.no</th>
<th>Order / Product No.</th>
<th>Product Name</th>
<th>Category</th>
<th>Product Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach($productList as $key => $data)
<tr>
<td>{{ $key+1 }}</td>
<td>{{$data->product_no}}</td>
<td>{{$data->name}}</td>
<td>{{$data->CategoryData->name}}</td>
<td>{{$data->description}}</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>
</td>
</tr>
</tbody>
@endforeach
</table>
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.
<?php
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);
dd($Data);
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.
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.