Created
April 20, 2021 18:55
-
-
Save reinink/f2fb9409a37f91986cdb965ae7e80a1d to your computer and use it in GitHub Desktop.
Laravel - Recently Created
This file contains 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 | |
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