mardi 12 mars 2019

Laravel Eager Loading, multiple same hasOne relations

I have 2 simple models. First, one is called Builds and the second one is called SlotOptions. Each build can have like 5 assigned slots.

class BuildDB extends Model

And has 5 such relations slot1-5 and id changes to slot1-5_id

 public function slot1()
    {
        return $this->hasOne('\App\SlotOptions', 'id', 'slot1_id');
    }

In the controller I call it such way;

BuildDB::with([ 'slot1', 'slot2', 'slot3', 'slot4', 'slot5'])->find(5);

\App\SlotOptions model doesn't contain any extra coding.

This generates 5 "same" queries. - atm the eager loading would work if I get a list of builds and each slot would have whereIn clause, is it possible to have it a one big wherein the clause, or does it require to change the DB schema.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire