D
D
dfgdfgdfgdfgfg2019-12-09 16:49:10
Laravel
dfgdfgdfgdfgfg, 2019-12-09 16:49:10

How to display companies by the number of comments?

$top_companies =Company::query()
            ->where(...)    // где коментариев больше както так
            ->paginate(8);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
jazzus, 2019-12-09
@jazzus

Company::has('comments', '>', 1)

N
Nikolay Alekseev, 2019-12-09
@VariusRain

->where('Comments', '>', 8)
My answer is given exactly as the question is asked. However, in reality the comments and their number will not be stored in the same table. Most likely, there is some kind of table with a foreign key to the companies table, you need to JOIN this table and in the condition rely on the number of records in it, and not in the main table.

N
Nikolai Smirnov, 2019-12-09
@Bzdykin

  1. Select companies along with comments using eager loading
  2. You run through the elements of the collection and add a new attribute, for example, count, the value of which will be equal to the number of comments for this company
  3. Sorting a collection by the count attribute
  4. Cut out only the necessary elements from the collection
  5. Returning a view with a collection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question