Skip to content

Instantly share code, notes, and snippets.

@ctf0
Last active June 21, 2018 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctf0/b1c178ad5deae142ad4a8ed46d5b05c4 to your computer and use it in GitHub Desktop.
Save ctf0/b1c178ad5deae142ad4a8ed46d5b05c4 to your computer and use it in GitHub Desktop.

Dont

  • hash the id on either boot::retrieved or getIdAttribute() because delete() & 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment