Skip to content
Advertisement

How to encrypt id in URL laravel

I want to encrypt the id in URL I’ll show my controller code and route. I’ve already used Crypt::encrypt($id); in my controller but it’s not working properly so I’ve commented that line in my controller

this is my controller

public function update(TenderRequest $request,$id){
    $tender = TenderMaster::findOrFail($id);
    //Crypt::encrypt($id);

    if($request->extend_date < $request->end_date || $request->bid_status > 0){                   
        return 'unsuccess';
    } else{ 
        $transaction = DB::transaction(function () use($request,$tender,$id) {
            $tender->extend_date = $request->extend_date;
            $tender->remarks = $request->remarks;
            $tender->update($request->all());
        });

        return 'BID '.$tender->ref_no.' Succesfully Updated';
    }
}
 
                         }

this is my route

  Route::post('tender/update/{id}','TenderTenderMasterController@update')->name('bid.update');

this is my blade

<form action="{{route('bid.update' ,Crypt::encrypt('id'))}}" class="form-horizontal" id="bid-update" method="POST">
    {{ csrf_field() }}
    @method('POST')
    @include ('tender.form', ['formMode' => 'edit'])
</form>

Advertisement

Answer

Use laravel builtin encryption to achieve this:

While adding your route in frontend, encrypt id with encryption helper like this:

{{route('bid.update', encrypt($id))}}

Now, In your controller, decrypt the id you have passed.

public function update($id, Request $request){

    $ID = decrypt($id);
    $tender = TenderMaster::findOrFail($ID);

    ..
    ...
}

I hope you understand.

Here is the docs:

https://laravel.com/docs/6.x/helpers#method-encrypt

https://laravel.com/docs/6.x/helpers#method-decrypt

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement