Browsed by
Tag: observer

Laravel: Create Hashed ID for A Resource Using Observer

Laravel: Create Hashed ID for A Resource Using Observer

Install hashids/hashsids package Create a wrapper for hashids Create a helper for hashids Create a config file for hashids Create a migration, which add a hashslug field Create a helper to get slug name of the given class name (optional) Create an observer for hashids which monitor on creating new record – if there’s a hashslug column and it’s null, do create hashed id for the record. Register the observer to model in boot() method in your app/Providers/AppServiceProvider.php Now let’s…

Read More Read More

Laravel: Observer

Laravel: Observer

Assalamualaikum & Hi, Eloquent models fire several events, allowing you to hook into the following points in a model’s lifecycle: retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored. Events allow you to easily execute code each time a specific model class is saved or updated in the database. Based on events above, we can capture all the events mentioned above either by setup in model’s property called $dispatchesEventsor we can setup an observer to capture all the…

Read More Read More