Comments (2)
@LarryEitel It's a standard eloquent model events. This events are fired using laravel fireModelEvent method, as you can see here
from eloquent-tree.
After some trial and error I came up with the following:
In the model:
public static function updatedDescendants($callback, $priority = 0)
{
static::registerModelEvent('updatedDescendants', $callback, $priority);
}
app/Events/Backend/PlaceUpdatedDescendantsEvent.php
<?php
namespace App\Events\Backend;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class PlaceUpdatedDescendantsEvent extends Event
{
use SerializesModels;
public function __construct($modelItem)
{
\Log::info("PlaceUpdatedDescendantsEvent._construct");
$this->modelItem = $modelItem;
}
public function broadcastOn()
{
return [];
}
}
app/Listeners/Backend/PlaceUpdatedDescendantsListener.php
<?php
namespace App\Listeners\Backend;
use App\Events\Backend\PlaceUpdatedDescendantsEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Queue\ShouldQueue;
class PlaceUpdatedDescendantsListener
{
public function handle(PlaceUpdatedDescendantsEvent $event)
{
$modelItem = $event->modelItem;
var_dump('PlaceUpdatedDescendantsEvent');
//$modelItem->save();
\Log::info("PlaceUpdatedDescendantsEvent handled");
}
}
app/Providers/EventServiceProvider.php
<?php namespace App\Providers;
use App\Events\Backend\PlaceUpdatedDescendantsEvent;
use App\Listeners\Backend\PlaceUpdatedDescendantsListener;
use Xchg\PlaceModel;
use App\Services\Place;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider {
protected $listen = [
PlaceUpdatedDescendantsEvent::class => [
PlaceUpdatedDescendantsListener::class,
],
];
public function boot(DispatcherContract $events)
{
parent::boot($events);
PlaceModel::updatedDescendants(function($item)
{
\Event::fire(new PlaceUpdatedDescendantsEvent($item));
});
}
}
Please feel free to share any observations/refinements. :)
from eloquent-tree.
Related Issues (10)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquent-tree.