Laravel will replace hashids primary key for everyone

I am using laravel 4.2 and I want to use hashes instead of the primary key in the urls. It is easy to use with one record. If I use active loading, I need to go through all the models and replace the primary keys with hash identifiers.

For instance. For each post I need to change the post_id with a hashid. For every comment on the publication, I have to do the same. For each user comment, etc. Can I extend the model to return hashid by default?

+4
source share
2 answers

, mutators. , .

public function getHashidAttribute()
{
    return your_hash_function($this->attributes['id']);
}

hashid , , $post->hashid, $comment->hashid ..

+1

Route:: bind, , URL, .

Route::bind('post', function($value)
{
    return Post::where('hashid', $value)->first();
});

, "" ,

/admin/{post}/edit
+1

Source: https://habr.com/ru/post/1584235/


All Articles