Hi tmunzar, that is indeed a good question.
Laravel provides the touch()
function to update parent models' timestamps: http://laravel.com/docs/4.2/eloquent#touching-parent-timestamps
But I haven't tried it yet and don't know if it also fires the respective saving
events. It is worth a try, though.
You would use it by adding
protected $touches = array('profiles');
to your Location
model. This way you would only need one event listener and function definition in the Profile
model.
Thank for the reply sisou.
I tried this but it doesn't seem to call the "saved" model event. I even tested if it called the "updated" model event, but it doesn't.
Is there a way I can make some sort of listener for this? Somehow override the touch() function of the 'Profile' model or something?
I appreciate the help!
Ok, so I found a way to make this work.
If you want to 'touch' a related model using the 'protected $touches' way, then it doesn't call its respective 'saved' model event.
However if you do it the 'saved' event as follows, the model event gets called. (for the 'Location' model)
public static function boot() {
parent::boot();
static::saved(function($location)
{
$profile = $location->profile;
$profile->touch();
});
}
I have no clue why this is so but it works like a charm.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community