Search code examples
phpmysqleloquentlaravel-5.1observers

Laravel 5.1 - Model observer on relative models


Let's suppose I have the following related tables:

pages
----------------
id 
content

visitors
----------------
id
name

page_visitor
----------------
page_id
visitor_id

As you see can we have three tables, in which pages and visitors have a many-to-many relationship.

I have successfully implemented an observer class on the pages model class, and now, what ever I do to the pages table gets reflected somewhere else. Using the following code:

class ElasticsearchPageObserver
{
    private $elasticsearch;

    public function __construct(ESClient $client)
    {
        $this->elasticsearch = $client;
    }

    public function created(Page $page)
    {
        $params = $page->buildElasticsearchParams();
        $response = $this->elasticsearch->index($params);
    }

    public function updated(Page $page)
    {
        $params = $page->buildElasticsearchParams();
        $response = $this->elasticsearch->index($params);
    }
}

You get the idea, right?

As it turns out, when the tables that have a one-to-many relationship with the page table get updated, the above observer, observes it. But not for many-to-many relationships(like the above).

Well, now how do I go about this??? How to do the same thing when page_visitor gets updated?

Thanks


Solution

  • I ended up creating a method to use instead of calling attach on every related class.

    // in page:
    public function attachTo($to, $id)
        {
            switch($to) {
                case 'App\Models\Visitor':
                    $this->visitors()->attach($id);
                    break;
                case 'App\Models\Banner':
                    $this->banners()->attach($id);
                    break;
                case 'App\Models\AdBlock':
                    $this->adblocks()->attach($id);
                    break;
                default:
                    return;
            }
            $this->indexInElasticsearch();
        }