ganyicz / nova-callbacks
Add after-save callbacks to your Laravel Nova resources.
Installs: 84 542
Dependents: 0
Suggesters: 0
Security: 0
Stars: 20
Watchers: 1
Forks: 8
Open Issues: 1
Requires
- php: ^7.2.5|^8.0
- laravel/nova: ^3.0|^4.0
README
Missing Nova resource callback functions
This package adds support for defining callback functions directly inside your resource class.
use Illuminate\Http\Request; use Ganyicz\NovaCallbacks\HasCallbacks class User extends Resource { use HasCallbacks; public function fields(Request $request) { return []; } public static function beforeSave(Request $request, $model) { // Do something before the model is created or updated } public static function afterSave(Request $request, $model) { // Do something after the model is created or updated } public static function beforeCreate(Request $request, $model) { // Do something before the model is created } public static function afterCreate(Request $request, $model) { // Do something after the model is created } public static function beforeUpdate(Request $request, $model) { // Do something before the model is updated } public static function afterUpdate(Request $request, $model) { // Do something after the model is updated } }
Why?
Currently, if you want to do anything after your resource is saved, you have to define a model observer outside of the resource class. This just makes the code harder to find. This package is especially useful if you only need to do a simple logic after your resource is saved, as everything can be kept in one file.
To expand the possibilities, check out my other package Nova Temporary Fields which allows you to create custom fields that won't be persisted in your model and will only be available inside the callbacks.
Installation
You can install the package via composer:
composer require ganyicz/nova-callbacks
Usage
- Apply
HasCallbacks
trait on your resource. - Define one of the callback functions.
TIP: Apply the trait on your base Resource class inside your Nova folder so that the callback functions are available for you in every new resource.
Available callbacks
public static function beforeSave(Request $request, $model)
Called both before creating and updating the resource
public static function afterSave(Request $request, $model)
Called both after creating and updating the resource
public static function beforeCreate(Request $request, $model)
Called before creating a new resource
public static function afterCreate(Request $request, $model)
Called after creating a new resource
public static function beforeUpdate(Request $request, $model)
Called before updating an existing resource
public static function afterUpdate(Request $request, $model)
Called after updating an existing resource
How does it work?
The implementation is really simple.
Before Nova fills the model, we overwrite fill
and fillForUpdate
methods exposed by Laravel\Nova\FillsFields
trait to add a closure based model event listener. After the model is saved, we simply call the callback function defined inside your resource.
Keep in mind
As the implementation works on saved
model event, saving the model itself inside the callback function will throw you into infinite loop. If you really need to save your model inside the callback, use saveQuietly()
so that the event is not dispatched.
As mentioned above, the trait provided by this package overrides fill
and fillForUpdate
methods on your resource.