I am trying to get my incrementing IDs hashed in my URLs. I cant seem get the encoding right, i followed both this and this tutorial, which didn't help. I tried the code i have in getRouteKey()
in a controller, and it seems to be doing the job and hashing the ID as intended.
Anybody see what I am doing wrong?
public function getRouteKey()
{
$key = $this->getKey();
$hashids = new \Hashids\Hashids('MySecretSalt', 5);
return $hashids->encode($key);
}
UPDATE Then I use binding/decoding as below;
Route::model('property', Property::class);
Route::bind('property', function ($value, $route) {
$hashids = new \Hashids\Hashids('MySecretSalt', 5);
return $hashids->decode(intval($value))[0];
});
My route is;
Route::get(
'/{property}/vvv/',
'PropertyController@property_dashboard'
)->name('property.dashboard');
I'm guessing you are using a javascript front end and not blades so you don't have access to the Laravel route function. If thats true and you are using something like Ziggy to create your routes, you can always just pass the hashid into ziggy's route method. You can create a hashid attribute in your models to return to the front end then.