Skip to content

Instantly share code, notes, and snippets.

<?php
// GET /users/3kTMd
Route::get('/users/{id}', function($id) {
// $id is now an int
return User::find($id);
});
<?php
Route::bind('id', function ($id) {
return Hasher::decode($id);
});
<?php
$hashed_id = Hasher::encode(1);
$id = Hasher::decode($hashed_id);
<?php
use Hashids\Hashids;
class Hasher
{
public static function encode(...$args)
{
return app(Hashids::class)->encode(...$args);
}
<?php
class MyModel extends Model
{
public function history()
{
return $this->morphMany(History::class, 'my_model_table', 'reference_table', 'reference_id');
}
}
<?php
class MyModelObserver
{
use TracksHistoryTrait;
public function updated(MyModel $model)
{
$this->track($model);
<?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;
<?php
public function updated($model)
{
$this->track($model);
}
@joetannenbaum
joetannenbaum / create-history-table.php
Last active January 30, 2017 16:36
create history table
<?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');

Patient

{
  "resourceType": "Patient",
  "id": "1081332",
  "meta": {
    "versionId": "119",
    "lastUpdated": "2016-06-05T13:56:36.758+00:00"
  },