Answer the question
In order to leave comments, you need to log in
How to display records above other order by boolean?
Please help me solve the problem :) There are 3 models:
1. Company -> id: int, name: str, tariff_id: foreign key (tariff)
2. Review -> id: int, stars: int, company_id: foreign key (company)
3. Tariff -> id: int, name: str, top: boolean It is
necessary to display a list of companies, but sort them in such a way that companies with tariff -> top set to true come first, then sort companies in descending order of number of stars (review -> stars) using AVG.
There is such a code, sorting in descending order of media. the meaning of the stars:
$companies = Company::leftJoin('reviews', 'reviews.company_id', '=', 'companies.id')
->select(['companies.*', DB::raw('AVG(stars) as r_stars')])
->groupBy('id')
->orderBy('r_stars', 'DESC')->with('tariff')->paginate($output);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question