Home > OS >  foreach returns null value in eloquent
foreach returns null value in eloquent

Time:08-24

i have a relationship object and set its resource in my controller :

$h_attendance=HourAttendance::with('adm','paper','faculty','program','student')
                                    ->whereRelation('adm','batch_id',$batch)
                                    ->whereMonth('date',$month)
                                    ->whereYear('date', $year)
                                    ->get();                
        
$data=HourAttendanceResource::collection($h_attendance);

this is my HourAttendanceResource file :

public function toArray($request)
    {
        $f=$this->student->first_name ?? '';
        $m=$this->student->middle_name ?? '';
        $l=$this->student->last_name ?? '';
        return [
            'student_id'=>$this->student_id,
            'admission_number'=>$this->adm->admission_number,
            'std_name'=> $f.$m.$l,
            'roll_no'=>$this->adm->roll_no ?? $this->adm->admission_number,
            'register_number'=>$this->adm->register_number,
            'id'=>$this->id,
            'date'=>$this->date,
            'time_slot_id'=>$this->time_slot_id,
            'program_type'=>$this->program_type,
            'program_id'=>$this->program_id,
            'attendance'=>$this->attendance,
            'user_id'=>$this->user_id,
            'timestamp'=>$this->timestamp,
            'faculty_name'=>$this->faculty->faculty_name,
             'attendancev'=>$this->when($this->attendance,function()
                            {
                                switch($this->attendance){
                                    case(1): $att='P'; 
                                    break;
                                    case(2): $att='L';
                                    break;
                                    case(0): $att='A';
                                    break;
                                    default: $att='';
                                    break;
                                 }
                                 return $att; 
                            }) ?? '',
            'paper_name'=>$this->when($this->program_type != null,$this->program_type == 1 || $this->program_type == 3 ? $this->paper->paper_name ?? null : null),
            'paper_teacher'=>$this->when($this->program_id != null,$this->program_type==1 || $this->program_type==3 ? $this->faculty->faculty_name:null ?? null),
            'paper_teacher_id'=>$this->when($this->program_type != null ,$this->program_type==1 || $this->program_type==3 ? $this->paper->faculty_id ?? null: null ),
            'programe_name'=>$this->when($this->program_type != null,$this->program_type==2 ? $this->program->program_name ?? null: null) ,
            'programe_description'=>$this->when($this->program_type != null,$this->program_type==4 ? $this->program->description ?? null: null) ,
            'is_marked'=>1,
            'day_name'=>carbon::parse($this->date)->dayName,
        ];
    }

but when i do

foreach($data as $dat){
            $day[]=$dat->day_name;  
        }
return dd($day);

it always returns null !

but there are data in my fields , they are not null.

following is the response when i do return $data; :

{ "student_id": 1370, "admission_number": "D1010", "std_name": "Ahsana Sherin", "roll_no": 9, "register_number": "DM1001", "id": 325, "date": "2022-07-01", "time_slot_id": 2, "program_type": 3, "program_id": 911, "attendance": 1, "user_id": 1131, "timestamp": "2022-07-01 15:11:54", "faculty_name": "AMAL Raj", "attendancev": "P", "paper_name": "New course01", "paper_teacher": "AMAL Raj", "paper_teacher_id": 1096, "programe_name": null, "programe_description": null, "is_marked": 1, "day_name": "Friday" },

hope somebody can find a solution ! thanks :)

CodePudding user response:

did u declare $day outside foreach loop? do u want to push all elements of foreach loop to $day or just last? because code above will overwrite $day each time with new values... guessing that u want to push all desired values in $day, maybe better structure of what you want to do is:

$day = [];

foreach($data as $dat)
{
    array_push($day, $dat->day_name);
}

CodePudding user response:

First of all you are not returning your array, but return of dd(...)

dd(...) doesn't have return value so no wonders it's null.

Second:

return type of a resource is an array so you should access keys like

$day[]=$dat['day_name'];

Third:

Please don't name your variables like std_name. You have student_id and std_name these inconsistencies will only make your code less readable.

Rule of thumb: Don't shorten names in your variables

  • Related