All individual subscriptions are 50% off this week only!

mohammed_alama
2 months ago
233
6
Laravel

Overwrite order in SqlQuery

Posted 2 months ago by mohammed_alama

Hi I'am in lesseon 17 from forum series when @Jeffrey_way make popular filter he write

protected function popular(){
     $this->builder->getQuery()->orders = [];
     return $this->builder->orderBy('replies_count','desc');
}

to overwrite this

 $threads = Thread::latest()->filter($filters);

the problem when go to url

forum.test/threads?popular=1

apply

dd($threads->toSql());

it give me that query

"select `threads`.*, (select count(*) from `replies` where `threads`.`id` = `replies`.`thread_id`) as `replies_count` from `threads` order by `created_at` desc"

it supposed to overwrite

order by 'created_at' desc

and make it

order by 'replies_count' desc

BTW Laravel is v6

Please sign in or create an account to participate in this conversation.