Questions & Answers

Control the run order of Eloquent scopes

I have a simple Eloquent scope that I would like to always run after all the other "Where" statements in the final query to optimize the efficiency of the query. I would like it to run last, no matter where I put it in the query builder (it's for a large application where this scope has been used many times, and I'd like it to be fool-proof in the future)

I'll put the scope below, but it's exactly the same as the example in the Eloquent documentation.

    public function scopeActive($query)
        $query->where('active', 1);

I know eloquent is smart enough to put ordering statements after Where statements, so maybe there's a trick to use that logic to manipulate the order of the where statements, but so far I can't see it. I'm using MySQL if there's some kind of raw sql I can run.

2023-01-20 23:30:03
Answers(0) :