Giter Site home page Giter Site logo

Comments (2)

AdrianSkierniewski avatar AdrianSkierniewski commented on July 18, 2024

@LarryEitel It's a standard eloquent model events. This events are fired using laravel fireModelEvent method, as you can see here

from eloquent-tree.

LarryEitel avatar LarryEitel commented on July 18, 2024

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 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.