Laravel model relationships and model events

Udders

I am building a notification system at the moment, and the notifications get delivered via model events. Some of the notifications are dependent on things happening with the models relationships.

Example: I have a project model that has a one:many relationship with users,

public function projectmanager() {
    return $this->belongsToMany('User', 'project_managers');
}

I am wanting to monitor for changes on this relationship in my project model events. At the moment, I am doing this by doing this following,

$dirtyAttributes = $project->getDirty();
foreach($dirtyAttributes as $attribute => $value) {
   //Do something
}

This is run in the ::updating event of the model but only looks at the models attributes and not any of it's relational data, is it possible get old relational data and new relational data to compare and process?

Leith

You should be using an observer class for this.

This has already been covered fairly simply and well by this SO answer, although that answer uses a slightly older method where the class itself needs to call upon its observer. The documentation for the current version (5.3 as of this answer) recommends registering the observer in your application service provider, which in your example would look similar to:

<?php
namespace App\Providers;

use App\Project;
use App\Observers\ProjectObserver;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Project::observe(ProjectObserver::class);
    }
}

For evaluating the differences between the new model values and the old values still in the relational DB, Laravel provides methods for both: getDirty() and getOriginal().

So your observer would look something like:

<?php

namespace App\Observers;

use App\Project;

class ProjectObserver
{
    /**
     * Listen to the Project updating event.
     *
     * @param  Project $project
     * @return void
     */
    public function updating(Project $project)
    {
        $dirtyAttributes = $project->getDirty();
        $originalAttributes = $project->getOriginal();
        // loop over one and compare/process against the other
        foreach ($dirtyAttributes as $attribute => $value) {
            // do something with the equivalent entry in $originalAttributes
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Category Model Relationships

From Dev

Reverse model relationships in Laravel

From Dev

Laravel Eloquent model events

From Dev

Laravel Multiple Model Events

From Dev

Laravel model relationships Class not found

From Dev

Laravel - dd() model with all relationships

From Dev

Laravel return model relationships in JSON

From Dev

Laravel Model Events dont fire

From Dev

laravel 4 mockery mock model relationships

From Dev

Laravel eloquent: Update A Model And its Relationships

From Dev

Laravel 5.1 testing with model relationships and factories

From Dev

Laravel 4: Model Relationships Not Working (sort of)?

From Dev

laravel 4 mockery mock model relationships

From Dev

How does it works the Laravel 4 model relationships?

From Dev

Laravel 5.1 testing with model relationships and factories

From Dev

Laravel include relationships with model after update

From Dev

Where to Store Model Observers (Events) in Laravel 5

From Dev

How to use model events with query builder in laravel

From Dev

Order of Laravel 4 Eloquent Model Observer Events

From Dev

Using Model Events Listener in Laravel 5

From Dev

Laravel saving model without triggering events

From Dev

Order of Laravel 4 Eloquent Model Observer Events

From Dev

Nested Eloquent model events not firing in Laravel 5.2?

From Dev

Load all relationships for a model

From Dev

Phalcon: Model relationships with Namespaces

From Dev

Complex model relationships in django

From Dev

Model relationships in mgo

From Dev

Core data model - relationships

From Dev

Searching model based on relationships