0

I'm trying to use a scope that is loaded by a trait onto a specific model so it does it on every query from that model

BUT

The query isn't working

public function apply(Builder $builder) {

    $builder->where(function($q){$q->whereIn('region_id', array(1,2))
            ->orWhere('manager_id', 1);
    });
}

This is what it is supposed to do but the page doesn't load, so I checked apache error logs and got a :

[core:notice] [pid 949] AH00051: child pid 2647 exit signal Segmentation fault (11), possible coredump in /etc/apache2

BUT if I apply the same creteria to just a query, then it works :S

Client::where(function($q){
        $q->whereIn('region_id', array(1,2))
          ->orWhere('manager_id', 1);
})->get();

So what is wrong here? It looks like the builder doesn't like taking functions, because it doesn't matter what I put in the function, it just doesn't work

2 Answers 2

3

Solution for you - use whereNested:

public function apply(Builder $builder) {

    $builder->whereNested(function ($q) {
          $q->whereIn('region_id', array(1,2))
            ->orWhere('manager_id', 1);
    });
}

Explanation:

$builder->where 

Eloquent\Builder method, causing infinite loop, because it's calling new query without scopes, which calls new query, which first boots scopes...

While:

$builder->whereNested

underyling Query\Builder method, that works as it is supposed to. The function call is forwarded to Query\Builder object, because of lack of whereNested method on the Eloquent\Builder, and magic __call method.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm not expert for this but in the first solution you don't have $q defined I think so it won't work. You could try with:

public function apply(Builder $builder) {

    $builder->whereIn('region_id', array(1,2))->orWhere('manager_id', 1);
}

or

public function apply(Builder $builder) {

    $builder->where(function() use $builder {$builder->whereIn('region_id', array(1,2))
            ->orWhere('manager_id', 1);
    });
}

2 Comments

The where stuff needs to be between (), that's why I'm using a function otherwise it's not possible in eloquent, when using a function it will put eveything in that function between ()
@Mazzy I've edited my answer, probably it won't work but you could try

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.