Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Eloquent Get Collection if it has at least one relation

I have a scenario where I have a model A that has 3 relations B, C, and D.
I’m trying to get collections in A where it has at least one relation
so I don’t want {id:1,B:[],C;[],D:[]} But I would accept {id:2,B:[{id:6}],C;[],D:[]}
I tried getting them by 3 has but that only gets it if it has all 3.
I also tried getting all then checking by if but I want a faster way.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use orWhereHas for that

$collectionA = A::where(function($query) {
        $query->whereHas('B')
            ->orWhereHas('C')
            ->orWhereHas('D');
    })
    ->get(); 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading