Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

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!

Last updated 1 year ago.
0

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.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

tmunzar tmunzar Joined 12 Sep 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.