E
E
Evgenii Borovoi2020-07-29 13:01:40
Laravel
Evgenii Borovoi, 2020-07-29 13:01:40

Is it possible in laravel to get an array through the query builder where the number of rows would be contained and the field by which it would be combined?

In general, here is a sql query, where in_week is a non-unique, random, repeating number.

SELECT in_week, count(*)  FROM hour GROUP BY in_week;


The table looks like this:
in_week | count |
78 | 3
79 | 15
136 | 2

We need an array where the index would be in_week, and the value would be count. I know how to make a method in a model via SQL, I wonder if it is possible on Eloquent queries.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2020-07-29
@EugeneOne77

QueryBuilder::get returns a collection that you can run through https://laravel.com/docs/5.8/collections#method-ma... and get what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question