C
C
codercat2016-09-12 13:31:47
Laravel
codercat, 2016-09-12 13:31:47

How is the duplication of these relationships organized?

There is a location object with a large number of relationships (region, district, town, house number, and so on)

class PropertyLocation extends Model
{
    public function district()
    {
        return $this->hasOne(LocationDistrict::class);
    }

    public function region()
    {
        return $this->hasOne(LocationRegion::class);
    }

    public function route()
    {
        return $this->hasOne(LocationRoute::class);
    }

    public function settlement()
    {
        return $this->hasOne(LocationSettlement::class);
    }
    ....
}

Since the location object itself is loaded by lazy loading and there are enough requests besides this, I would like to duplicate the relationship data in the location object itself. That is, the output should be a location object, in which, for example, name and id from the related tables are duplicated: region_id ,
region_name etc. Googling didn't help :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Tokarev, 2016-09-12
@codercat

As I understand it, there is nothing special, as you wrote, store fields other than region_id, also region_name, etc. In order for everything to be updated correctly, you can hang a trigger to save the location and check if $location->isDirty(['region_id']), then update region_name before saving to the database.
To update the region and the like, you can also hang a trigger in Laravel, or you can directly on the base, and use this trigger to update the names of all locations tied to it.
PS. As far as I understood, you should have belongsTo instead of hasOne.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question