I want to pass encrypted ID as route parameter. Currently, I'm using Route model binding and it returns a 404
Asked
Active
Viewed 535 times
-2
-
Can you share code what you tried? – divyesh makvana Feb 18 '22 at 05:15
-
Have you decrypted the id in controller? – Tamim Feb 18 '22 at 05:21
1 Answers
1
You can create a custom resolver for that modal like
use Illuminate\Support\Facades\Crypt;
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($encryptedId, $field = null)
{
return $this->where('id', Crypt::decryptString ($encryptedId))->firstOrFail();
}
in the above code you can get the encrypted
data as $encryptedId
you can decrypt and used in query to perform search
same thing you can do in RouteServiceProvider
use App\Models\User;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Crypt;
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
Route::bind('user', function ($encryptedId) {
return User::where('id', Crypt::decryptString ($encryptedId))->firstOrFail();
});
// ...
}
ref link https://laravel.com/docs/9.x/routing#customizing-the-resolution-logic

Kamlesh Paul
- 11,778
- 2
- 20
- 33
-
2Similar question Found here my be helpful https://stackoverflow.com/questions/71163085/laravel-route-model-binding-shows-404-when-i-passed-encrypted-id-for-editing/71167773#71167773 – Hemil Feb 18 '22 at 07:45