Giter VIP home page Giter VIP logo

nova-callbacks's Introduction

Banner

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

  1. Apply HasCallbacks trait on your resource.
  2. 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.