Download the PHP package fico7489/laravel-updated-related without Composer
On this page you can find all versions of the php package fico7489/laravel-updated-related. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fico7489/laravel-updated-related
More information about fico7489/laravel-updated-related
Files in fico7489/laravel-updated-related
Package laravel-updated-related
Short Description Update Elasticsearch data or clear cache when the model or any related model is updated, created or deleted.
License MIT
Homepage https://github.com/fico7489/laravel-updated-related
Informations about the package laravel-updated-related
Laravel Updated Related
Update Elasticsearch data or clear cache when the model or any related model is updated, created or deleted.
Why to use
In laravel, we have the ability to listen to changes on eloquent models(create, delete, update), and when the model is changed we can do something with model e.g. we can flush cache for this model.
But sometimes we want to do something with the model when this model is changed or related models are changed. For example, you have model Seller with related models Order, ZipCodes, Address and OrderItem. If any change on Seller, Address, Order, ZipCode or OrderItem is performed you want to do something with Seller e.g flush cache for seller because cache include related models data:
In above case when Seller model is changed, we have to flush his cache 'seller'.$id but also when his particular related models are updated we have to flush data. Obviously we need some configuration with related models here and this package will help you with this.
You can add this functionality without the package, but there is another huge problem. You can map related models and parent model to know when to flush cache but if you have batch form, e.g. for update all ZipCodes, and if you have 1000 zip codes and hit save, cache will be flushed 1000 times. This is not a problem for a flushing cache but if you have to update Elasticsearch data or perform some other time-consuming operation then it is.
This package also solves above problem because model changed events are saved to an array and processed after the request when they are filtered to be unique, so if you update 1000 zip codes for the same seller only one event will be dispatched.
Version Compatibility
The package is available for larvel 5.* versions.
Install
1.Install package with composer
2.Add service provider to config/app.php
3.Publish configuration
and after that adjust configuration(map model with related models), see more in below section.
4.Use this trait
in your base model.
and that's it, you are ready to go.
Configuration
Configuration is located at config/laravel-updated-related.php
1.You can create configuration in simple way :
KEYS are base models, VALUES are arrays with related model => relation
2.Or you can create configuration in detailed way if you need more environments for the same base model. :
KEYS are base models, VALUES are arrays with names (environment name) and related configuration (arrays with related model => relation). In "simple way" environment will be 'default'.
After you set a configuration just listened to the ModelChanged event that will be dispatched when any model or its related model (defined in configuration) is changed (updated, deleted, created).
One real example
Configuration :
Models :
Event service provider :
Listener :
With above code you will se this output:
If you create, delete or update User or Address model ModelChanged will be dispatched.
Cover all changes in the database
Do not use code like this one:
Because in that case, laravel does not use a model, it runs query directly without a model. To cover above changes you can change this code to :
Pivot events
If you want to cover pivot events use this package : https://github.com/fico7489/laravel-pivot
When to use this package
- clear cache for model
- update elasticsearch data
- and much more
License
MIT
Free Software, Hell Yeah!