How do I get a “pivot table” model to trigger model save / save events when an application or detach is called in Laravel?

In Laravel 4, how do I get a model pivot tableto trigger saved / saved model events when connecting or disconnecting?

It seems that the “TeamUser” pivot table below is not really needed for the attach / detach methods to work, so assuming I believe that the model representing the pivot table is never called. And so events never fire.

To ask another way: When I call User::with('Team')->find(1)->teams()->attach(1);, how to get TeamUserto trigger his own events. Please note that the above application works fine, all records are updated in the database.

User

class User extends Eloquent 
{
    // Relationship
    public function teams() 
    {
        return $this->belongsToMany('Team');
    }    
}

class Team extends Eloquent 
{
    // Relationship
    public function users() 
    {
        return $this->belongsToMany('User');
    }    
}

. TeamUser /

class TeamUser extends Eloquent 
{
    public function save(array $options = array())
    {
        // do cool stuff when relationship is saved via
        // attach/detach
    }
}
+4
2

$touches Team.

class Team extends Eloquent 
{
    protected $touches = array('TeamUser');

    // Relationship
    public function users() 
    {
        return $this->belongsToMany('User');
    }    
}

TeamUser updated_at. , save() TeamUser.

, Team save() - .

Event::fire('team.updated', array($team));
+5

All Articles