08 August 2017

Laravel Trik - Pengganti UUID sebagai unique primary key


This is a little example code for a substitute uuid.

<?php /** * Attach a hash to the user model. This way we dont need a UUID/GUID. Keep * in mind that the HASH will change every time when you instantiate the * User model * * Class User */ class User extends Model { protected $appends = [ 'hash' ]; /** * @return string */ public function getHashAttribute() { return encrypt( $this->id ); } } ?> <?php # Route Route::get( 'user/{user_hash}/show', [ 'as' => 'user.show', 'uses' => 'UserController@show' ] ); # Model Binding /** * @throws \Illuminate\Contracts\Encryption\DecryptException */ app( 'router' )->bind( 'user_hash', function ( $hash ) { return app( User::class )->findOrFail( decrypt( $hash ) ); } ); ?> <!-- Create a link --> <a href="{{route('user.show', [$user->hash])}}">Edit</a>

im getting from http://laravel-tricks.com/tricks/no-more-uuidguids.

1 comment:

Anonymous said...

이 보고서는 요청에 대한 COVID-19 운명에 대한 분석을 제공합니다. 연도별로는 2017년 503건이었던 청소년 온라인 불법도박 상담건수는 2018년 1,027건, 2019년 1,459건, 2020년 1,286건, 2021년 1,242건으로 가파르게 증가하고 있다. 의 모든 기사의 사진과 텍스트는 상업적인 용도로 일부 혹은 전체를 무단 전재할 수 없습니다. 링크를 걸거나 SNS 퍼가기 버튼으로 공유해주세요. 나무 향을 맡으면 저절로 마음이 차분하게 가라앉아요. 외부 환경이 아무리 소란스럽더라도 말없이 고요하게 놓여 있는 나무를 바라보다 보면 말로 표현할 수 없는 울림이 전해지는 것만 같아요.