Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mickie's avatar

Add additional field in collection on the basis of values in other fields

i am new to laravel and still learning online and from docs. working on student attendance system . students data is saved in 'students' table and attendance data is saved in 'attendances' table linked to students with student_id. Using this code to fetch students and their attendance:

$students = DB::table('students as s')
                    ->rightJoin('attendances as a', 'a.student_id', '=', 's.id')
                    ->rightJoin('class as c', 'c.id', '=', 's.clas_id')
                    ->rightJoin('sections as sec', 'sec.id', '=', 's.section_id')
                    ->select('s.id', 's.acc_id','s.roll_no', 's.name', 's.fname','s.mobile1','s.mobile2',' a.att_date', 'a.clas_name' ,'a.section_name', 'a.status','a.leave_sent','a.remarks')
                    ->where('a.att_date','=', $date)
                    ->where('c.id','=', $c)
                    ->where('sec.id','=', $s)
                    ->orderBy('s.roll_no','ASC')
                    ->get(); 

I want to load additional field message in this collection e.g.

 if($status == "P" ) {
      $message = $student->name.  'is PRESENT today.';
} else  {
    if($leave_sent=="") {
      $message = $student->name. '  is ABSENT  today and.. .............. .';
    } else {
      $message = $student->name. '  is ABSENT today and ................. .';
    }
}

in blade file i successfully output the field in table. But i want to add the message in collection itself (for further procession and send message in loop) From docs found out about PUT method $students = $students->put('message', $message);

Now i am confused about usage of this. Can be used in controller before calling view or we can also add this directly in blade with in for each loop? What is the correct method to add addition field, pls someone help.

0 likes
0 replies

Please or to participate in this conversation.