-1

I need to do a grouping by date. The problem that are two tables "kanban" and "apontamentos".

something like: Kanban::with('apontamentos')->get()->groupBy('dataApontamento'); property error

enter image description here

I've tried everything, use "groupBy" in the model. Need to know how to group in the controller and do the "foreach"

closest I ever got was that way:

$days = Kanban::with('apontamentos')->get()->groupBy(function ($val) {
      return $val->dataApontamento;
  });
  // dd($days);

  foreach ($days as $day => $appointments) {
    echo '<h2>'.$day.'</h2><ul>';
    foreach ($appointments as $appointment) {
        echo '<li>'.$appointment->atividade.'</li>';
    }
    echo '</ul><br>';
  }

clearer example:

Kanban

Title: waiting for service

Apontamentos

date 05/05

Project1

Project2

Project3

date 06/05

Project1

Project2

Project6

Project7

Kanban

Title: pending

apontamentos

date: 08/05

Project9

Project8

Project4

Kanban

Title: Kanban

Title: waiting for service

apontamentos

date: 09/05

Project1

Project1

Project1

Project1

10/05

Project1

Project1

Project1

Project1

1 Answers1

0

try this!

Kanban::with(['apontamentos' => function($query){
    $query->groupBy('dataApontamento');
}])->get();
Alex Guerrero
  • 229
  • 2
  • 8
  • thanks @Alex for responding, gave this error. SQLSTATE[42000]: Syntax error or access violation: 1055 'company.apontamentos.id' isn't in GROUP BY (SQL: select * from `apontamentos` where `apontamentos`.`kanban_id` in (7, 10, 11, 12) group by `dataApontamento`) – Felipe Gabriel May 11 '20 at 18:11
  • you have to put company.apontamentos.id in the group by or you have to tell laravel to use strict mode false for the database you have to go to config/database and in the mysql put this 'strict' => false – Alex Guerrero May 11 '20 at 18:39
  • same mistake... `'prefix' => '', 'prefix_indexes' => true, 'strict' => false, 'engine' => null,` – Felipe Gabriel May 11 '20 at 18:44
  • check in you db if your mysql have the stict mode to fale with SELECT @@GLOBAL.sql_mode and you can change it by SET @@GLOBAL.sql_mode=''" – Alex Guerrero May 11 '20 at 18:50
  • how to explain it better? I use xampp – Felipe Gabriel May 11 '20 at 18:56
  • check this https://stackoverflow.com/questions/40881773/how-to-turn-on-off-mysql-strict-mode-in-localhost-xampp – Alex Guerrero May 11 '20 at 19:00