How to retrieve fields with a count field

Posted 2 years ago by thiagocardoso

Hello everybody,

What I have:

  • code: Escala::select(['name', 'data_inicio'])->groupBy(['name', 'data_inicio'])
  • raw SQL (with toSql()) : "select`name`,`data_inicio`from`escalas`groupby`name`,`data_inicio`"
  • result (with get()):
[
  {
    "name": "a",
    "data_inicio": "2017-03-16 10:00:00"
  },
  {
    "name": "a",
    "data_inicio": "2017-03-17 10:00:00"
  }
]
  • full data :
[
  {
    "id": 1,
    "name": "a",
    "data_inicio": "2017-03-16 10:00:00",
    "user_id": 2
  },
  {
    "id": 2,
    "name": "a",
    "data_inicio": "2017-03-16 10:00:00",
    "user_id": 3
  },
  {
    "id": 3,
    "name": "a",
    "data_inicio": "2017-03-17 10:00:00",
    "user_id": 2
  },
  {
    "id": 4,
    "name": "a",
    "data_inicio": "2017-03-17 10:00:00",
    "user_id": 3
  }
]

what I'm trying to get is the same output, but with a field with the count of the rows (like this)

[
  {
    "name": "a",
    "data_inicio": "2017-03-16 10:00:00",
    "count": 2
  },
  {
    "name": "a",
    "data_inicio": "2017-03-17 10:00:00",
    "count": 2
  }
]

How to get it? Should I use raw statements?

Please sign in or create an account to participate in this conversation.