michaelvolst
2 years ago

Transpose data from database

Posted 2 years ago by michaelvolst

Hello,

I would like to tranpose my data to a html table and there for I have to change the rows to columns. I couldn't get this to work and I have found this post on the blog from adam wathan. link

I hope someone can help me with this and if you need more information, just let me know.

I have two tables called days and logs. The table days has many dayslogs.

This is the array how my days looks like

0 => array:13 [▼
    "id" => 99
    "status" => 1
    "location_id" => 1
    "user_open_id" => 31
    "user_close_id" => 13
    "opmerking" => ""
    "sluitings_tijd" => "2017-02-16 14:09:28"
    "day_number" => 3
    "created_at" => "2017-02-15 16:02:21"
    "updated_at" => "2017-02-16 14:09:28"
  ]

And this is how my array of the dayslogs looks like

logs

array:5 [▼
  0 => array:86 [▼
    0 => array:17 [▼
      "id" => 7404
      "task_id" => 40
      "dag_id" => 99
      "department_id" => 18
      "user_id" => 31
      "klaar" => 1
      "status" => 0
      "url_foto" => null
      "opmerkingen" => null
      "created_at" => "2017-02-15 16:02:21"
      "updated_at" => "2017-02-15 21:45:34"
      "meer_info" => 1
      "naam" => "Entree checken op vuil"
      "omschrijving" => ""
      "afbeelding_verplicht" => "0"
      "afbeelding" => ""
      "order" => 37
    ]
    1 => array:17 [▼
      "id" => 7405
      "task_id" => 41
      "dag_id" => 99
      "department_id" => 18
      "user_id" => 31
      "klaar" => 1
      "status" => 0
      "url_foto" => null
      "opmerkingen" => null
      "created_at" => "2017-02-15 16:02:21"
      "updated_at" => "2017-02-15 16:02:27"
      "meer_info" => 1
      "naam" => "Alarm er af halen"
      "omschrijving" => ""
      "afbeelding_verplicht" => "0"
      "afbeelding" => ""
      "order" => 0
    ]

And this is how my code looks like right now when I am tranposing the array


    $logs = [];

    foreach($draaidagen as $draaidag) {
      $logArray = $draaidag->draaidaglog->toArray();
      $logs[] =  collect($logArray)->transpose()->map(function($data) {
        return $data;
      });
    }

    dd($logs);


How the table should like

Log, day1, day2, day3, day4, day5

Log √ x √ x x

Log x √ x x √

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