Dont
-
hash the id on either
boot::retrieved
orgetIdAttribute()
becausedelete() & forceDelete()
wont work as they create a new query which will keep getting the same hashed id -
use
Route::bind
in the service provider as its applying the bind globally.
Maybe
- save the hashed id to the db same as
UUID
but if u r using mysql, indexing will suffer - you have to change all migrations foreign_ids type
Do
- keep everything as it is & add a new attribute to the model
public function getEncIdAttribute()
{
return app('optimus')->encode($this->id);
}
- and use it instead of normal id
// before
$model->id
// after
$model->encId
- inside the model controller use
public function __construct()
{
app('router')->bind('id', function ($id) {
return app('optimus')->decode($id);
});
// ...
}
now eveything works as expected and the id gets decoded back to its original state once it arrives to the controller.