Skip to content

Instantly share code, notes, and snippets.

@reinink
Created April 20, 2021 18:55
Show Gist options
  • Save reinink/f2fb9409a37f91986cdb965ae7e80a1d to your computer and use it in GitHub Desktop.
Save reinink/f2fb9409a37f91986cdb965ae7e80a1d to your computer and use it in GitHub Desktop.
Laravel - Recently Created
<?php
namespace App\Models;
use Illuminate\Support\Facades\Session;
use Illuminate\Database\Eloquent\Model as Eloquent;
abstract class Model extends Eloquent
{
protected $guarded = [];
protected static function booted()
{
static::created(function ($model) {
Session::flash('recentlyCreated.'.class_basename(static::class), $model->id);
});
parent::booted();
}
protected static function recentlyCreated()
{
if ($id = Session::pull('recentlyCreated.'.class_basename(static::class))) {
return static::find($id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment