-1

i need to count all records in my table, but in the mp_id column there are repetitions

how to count only once where the content of mp_id is repeated?

so that the count is not wrong, i want the mp_id to count only once and not 3 or 4.

  $ingresos = Ingresosmp::All()->count('mp_id');

enter image description here

  • Can you please rephrase what you're trying to achieve? – Daniel L Aug 29 '22 at 22:04
  • Sounds like you first want to get all unique cases of mp_id and count those. Check the docs for a unique() method to call before count. – CFulford Aug 29 '22 at 22:05
  • @DanielL I want to count the mp_id only once, but there are several repeated mp_id, I need to filter so that it only counts them once – Jeremias Rosas Aug 29 '22 at 22:13
  • Does this answer your question? [Laravel Distinct Count](https://stackoverflow.com/questions/35868551/laravel-distinct-count) – N69S Aug 29 '22 at 23:00

1 Answers1

0
$ingresos = Ingresosmp::All()->unique('mp_id');
$ingresos = count($ingresos);

Verify results with:

dd($ingresos);

Edit: you can condense this into a single line of code, this is just for readability. You can do this by:

$ingresos = count(Ingresosmp::all()->unique('mp_id'));
Andrew Godby
  • 52
  • 1
  • 8
  • That's a bad solution since you get all the data from database before looping the result models to generate the unique array then count it. you can directly count from database https://stackoverflow.com/questions/35868551/laravel-distinct-count – N69S Aug 29 '22 at 23:01