Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

farshadf's avatar

how to make laravel local scope just query the current model and relation not all of it

i am using spatie query builder and i am using the scopes query now this is my local scope i added :

  public function scopeFilterPrice(Builder $query,$start_price) : Builder
    {
        $data = $query->whereHas('accommodationRoomsLimited.roomPricingHistorySearch', function(Builder $query) use ($start_price){
            $query->where('sales_price','<',$start_price);
    });
        return $data;

    }

when i add this query in my filter this takes about 40 secs because every time it searches all the models and get the result but every time i just want to look in the current model and when i change to code to this below :

            $this->where('sales_price','<',$start_price);

the performance gets back to less than 1 sec but the query is not working so what i am doing wrong here any idea ?

0 likes
0 replies

Please or to participate in this conversation.