ErrorException : Undefined offset: 0

Viewed 256

on my array push i want to concat the 2 data if they have the same date

$goal = Goal::where('employee_id',Auth::user()->employees->first()->id)
->with('accomplishments')->orderBy('date','asc')->get();

    $next_week = $goal->whereBetween('date',[$add_start_date,$add_end_date]);
    $last_week = $goal->whereBetween('date',[$sub_start_date,$sub_end_date]);

$goals = [];
$date = "";
        for ($i=0; $i < count($next_week); $i++) { 
                if($next_week[$i]['date']==$date){
                    $goals[$i-1]['activity'] = $goals[$i-1]['activity'] .', '. $next_week[$i]['activity'];
                    continue;
                }
                array_push($goals,$next_week[$i]);
                $date = $next_week[$i]['date'];
    }
1 Answers

when using filtering on laravel collections, indexes are lost, to re_index the result array, use 'values':

 $next_week = $goal->whereBetween('date',[$add_start_date,$add_end_date])->values();
    $last_week = $goal->whereBetween('date',[$sub_start_date,$sub_end_date])->values();
Related