0

I am using laravel 5.4. Imagine I want to show some car advertising. I will get all car records from a table and load them into my page. Now I want to show multiple categories with counts in a sidebar (car types, car colors, car fuels) and I want to show the count for every category.

For example: we have 10 car ads, two of them are red, three are green and five of them are white. I want to have something like this in a sidebar:

red(2)
green(3)
white(5)

I did it with SQL groupBy:

        $colors = DB::table('advertisments')->select('color_name', DB::raw('count(color_name) as total_color'))->groupBy('color_name')->get();
        $gearboxes = DB::table('advertisments')->select('gearbox', DB::raw('count(gearbox) as total_gearbox'))->groupBy('gearbox')->get();
        $param = ['colors' => $colors,'gearboxes' => $gearboxes];
        return view('ads' , compact('ads','param'));

However, I have tens of categories and I can't do it with one groupBy so the number of queries is going to be too high. Is there a way to do it with a single query or with less queries?

2
  • what do you want? sql query with group by or laravel code ? Commented Oct 3, 2017 at 7:39
  • That does not matter, i just want to do that. But I think it's only possible with sql and group by Commented Oct 3, 2017 at 7:41

2 Answers 2

2

You could use an array for make it easy to run through your different columns.

$params = [];
$columns = [
    'colors' => 'color_name',
    'gearboxes' => 'gearbox',
    // put your other columns in this array
];

foreach($columns as $name => $column)
    $params[$name] = DB::table('advertisments')->select($column, DB::raw('count(' . $column . ') as total'))->groupBy($column)->get();

return view('ads' , compact('ads','param'));
Sign up to request clarification or add additional context in comments.

5 Comments

thank you . its less code but it is still tens of queries. is there any way to Decrease queries??
I don't believe as long it's different columns. You could use subqueries, but those still count as a query. You will lose the waiting for the respond of the database every time, but aslong your database is on the same server, or the same datacenter as your webserver, I don't see a huge possible performance boost.
Ok so i will do this by multiple group by and i think its best way and not bad
Super, will you accept my answer then? A thing you could do, is to structure your table in a different way, but that is ofcourse a lot more work.
If you had a relation table with your properties, containing a column to tell what the property is, like "color", and then have a column with the value, then you could group by both columns. In that way you will get all counts in one query. But this ofcourse also means that your properties need the same column-type for your values.
-1
$allColors = Model::withCount(['advertisments_model', 'more_model', '...'])

4 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
How to grouping it with advertisments_model?
@MarkRotteveel Thanks to advice me
@RayCoder Thanks to ask me to the main point. Laravel help to us in buildin method with eloquent relationship it set in advertisements model like this : public function advertisements(){ return $this->belongsTo(product::class, 'id', 'product_id'); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.