This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class UserTransformer extends TransformerAbstract | |
{ | |
public function transform(User $user) | |
{ | |
return [ | |
// Make sure to hash any IDs before returning | |
'id' => Hasher::encode($user->id), | |
'first_name' => $user->first_name, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// GET /users/3kTMd | |
Route::get('/users/{id}', function($id) { | |
// $id is now an int | |
return User::find($id); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::bind('id', function ($id) { | |
return Hasher::decode($id); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$hashed_id = Hasher::encode(1); | |
$id = Hasher::decode($hashed_id); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Hashids\Hashids; | |
class Hasher | |
{ | |
public static function encode(...$args) | |
{ | |
return app(Hashids::class)->encode(...$args); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyModel extends Model | |
{ | |
public function history() | |
{ | |
return $this->morphMany(History::class, 'my_model_table', 'reference_table', 'reference_id'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyModelObserver | |
{ | |
use TracksHistoryTrait; | |
public function updated(MyModel $model) | |
{ | |
$this->track($model); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
trait TracksHistoryTrait | |
{ | |
protected function track(Model $model, callable $func = null, $table = null, $id = null) | |
{ | |
// Allow for overriding of table if it's not the model table | |
$table = $table ?: $model->getTable(); | |
// Allow for overriding of id if it's not the model id | |
$id = $id ?: $model->id; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function updated($model) | |
{ | |
$this->track($model); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CreateHistoryTable extends Migration | |
{ | |
public function up() | |
{ | |
Schema::create('history', function (Blueprint $table) { | |
$table->increments('id'); | |
// Which table are we tracking | |
$table->string('reference_table'); |