V
V
Vladimir Singhtlov2019-10-10 16:59:56
MySQL
Vladimir Singhtlov, 2019-10-10 16:59:56

Why is the correct connection not being used when requesting?

There are 2 databases:
db1 with the prices table and db2 with the diseases and model_price tables (link table)
The Price and Diseases models are polymorphically linked using the ModelPrice(MorphPivot) model

class Price extends Model
{
    protected $connection = 'db1'
    public function diseases(){
        return $this->morphedByMany(Disease::class, 'modelable', 'model_price')->using(ModelPrice::class);
    }
}

class Disease extends Model
{
    protected $connection = 'db2'
    public function prices(){
        return $this->morphToMany(Price::class, 'modelable', 'model_price')->using(ModelPrice::class);
    }
}

class ModelPrice extends MorphPivot
{
    protected $connection = 'db2'
}

When getting diseases related to the price , the request is formed correctly, but when I try to get the price for a specific disease , the Request is formed not as it should to db2.model_price, but to db1.model_price, and this table is not in that database, how to force the use of the correct connection and return normal data?
Price::find(1)->diseases
Disease::find(1)->prices

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question