Update model when relationship changes

I have 2 Laravel models, Team and Photo.

A team can have many photos and a photo belongs to a team.

As such I have 2 database tables for teams and photos.

The way I have it configured right now using Dropzone.js is that uploading a photo(s) hits a different endpoint than updating a team (even although both actions are called from the same page).

On Algolia, I’m only tracking teams and so I’m looking for a way to update the image a team has when a photo(s) gets uploaded.

Within Team.php I do this

return array_merge($this->toArray(), [
                'sport_id' => $this->sport->sport_name,
                'photo' => $this->photos()->first()->path

I need to be able to trigger this so that when a photo is uploaded, the path to the first photo in the associated team also updates.


I wrote some documentation about it here: https://www.algolia.com/doc/api-client/laravel/relationships/?language=laravel#updating-relations-when-parentchild-change

Please let me know if that helps or if you have further questions :slight_smile: