Help refactor my code

Posted 2 years ago by michaelvolst

I think this code can be written a lot better. But I'm not sure how. I would love to get some help with it.

   $tasks = $location->tasks;
     
    $table = [];
    $counter = 0;

    foreach($tasks->sortBy('name') as $task) {
      $table[] = [
          'task_id' => $task->id,
          'taak_name' => $task->name,
      ];
      $counter++;
    }
    // temporarily
    $startday = Carbon::parse('2017-03-19');

    $days = $location
                   ->days
                   ->where('created_at', '<=', $startday->toDateString())
                   ->where('created_at', '>=', $startday->subDays('7')->toDateString());  

    foreach($days as $day) {
      $logs = Log::where('day_id', $day->id)->get();

      foreach($logs as $log) {
        for ($i=0; $i < $counter; $i++) {
          $table[$i]['day_number'] = $day->day_number;
          $table[$i][$day->day_number]['finished'] = $log->finished;
          $table[$i][$day->day_number]['status'] = $log->status; 
        }
      }
    }

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