Download the PHP package ganyicz/nova-callbacks without Composer
On this page you can find all versions of the php package ganyicz/nova-callbacks. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package nova-callbacks
Missing Nova resource callback functions
This package adds support for defining callback functions directly inside your resource class.
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:
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.