dimanche 24 mars 2019

Refactor whereHas to use via global scopes in Laravel

I've 2 models, CoreChallenge and Challenge. Below are relations between table. relation between tables

Now I want only active core_challenges to be displayed. I can do that via global scope in CoreChallenge model. But when I'm getting challenge's it gives me null in relationship when Corechallenge is inactive.

I've done it this way

$challenges = Challenge::with('core_challenge')->whereHas('core_challenge', function($q){
            $q->where('status', '=', 'active');
        })->get();

I want to do it using global scopes

Global scope on CoreChallenge gives me null, but I want that it's parent (Challenge) should not load even, like in whereHas. Is there any way?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire