Problem with Count() in the Laravel

Asked

Viewed 40 times

-2

I need to show the number of records belonging to a registered mobile number.

in the log table have the columns NUMBER and MESSAGES need to show the user the amount of messages that the registered numbers have.

By a @foreach I show on the screen all the records of the LOGS table that are: NUMBER, REGISTRATION DATE AND MESSAGES

but the COLUMN MESSAGES I need to be summed all the lines that belong to the NUMBER X and printed on the screen the total.

I registered to test each number 2 times, so should show on the screen that all numbers have 2 messages.

but is returning me the number 7 which is the amount of records in TABLE LOGS.

I tried in many ways, but without success.

I currently have the code

$numero = '{{ $logs->numero }}';
                
                $teste = DB::table("logs")->select("mensagens")->where('numero', '<=', 

$numero)->count();

inserir a descrição da imagem aqui

  • Apply a grouping to the column numero, otherwise the count will return the quantity of the entire table.

  • $test = DB::table('logs') ->select('messages', DB::raw('Count(*) the numbers') ->groupBy('messages') ->get(); I tried that, but it didn’t work. Can you give me an example of your answer ? @Woss

  • I recommend reviewing the SQL materials you studied. You need to group by number, not by messages.

  • is sorry ignorance. I am beginner in Laravel. and I’m half lost.

  • You could do this directly in Mysql?

  • I had to go to the database.php file and make 'Strict' => false. with this I got a better result.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.