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

coder72's avatar

How to make in array format in laravel ?

In Student model, i have made attribute like this

 protected $appends = ['present'];

 public function getPresentAttribute(){
          return $this->getTotalDays()-$this->getAbsentDays();
 }

My Database looke like this

| id | student_name         |    created_at     | 
| 1  |  Adam                |     2020/11/10    | 
| 2  |  Annie               |    2020/11/10     |
|3   |  Paul                |  2020/11/10       |
From getPresentAttribute() 
i get data like this 
Adam -> 32
Annie ->34
Paul->33

So now how to convert this

        ['Students', 'Present Day',],
        ['Adam'     , 32],
        ['Annie'    , 34],
        ['Paul'     , 33],
        
public function attendancePerformance(){
  $students=Student::get();
     $student_name = [];
     $data=[];
     $arrayHeader = ["Students","Present Day"];
          foreach ($students as $stu) {
                array_push($student_name, $stu->student_name);
             
             }
  array_push($data, $student_name);
              foreach ($students as $stu) {
                array_push($data, $stu->present); //present attribute i have appended in model
             
              }
    }
}

I think this is not proper way.Can someone help me

0 likes
15 replies
automica's avatar
automica
Best Answer
Level 54

@coder72 you can do it quite cleanly with just a foreach:

function attendancePerformance()
{
    $students = Student::get();
    
    $data = [];

    $data[] = ["Students", "Present Day"];

    foreach ($students as $student) {
        $data[] = [$student->name, $student->present];
    }

    return $data;
}

you do get numeric keys with the returned data but that'd be fine if all you are doing is passing these to blade to display

I would also suggest you look at

$students = Student::all()->pluck('name','present');

as (if I am correct) this will return you a list of students name and present field.

1 like
coder72's avatar

@automica Thanks it worked actually but can i ask you something related this ?

coder72's avatar

This is controller.This function is for filtering So When user select some year.That year will come in controller but can i send those year in model for filtering?

function attendancePerformance(Request $request)
  {
      $year=$request->year;   //This is for filter where someone can choose year for filtering of absent year
      $students = Student::get();
    
      $data = [];

      $data[] = ["Students", "Present Day"];

      foreach ($students as $student) {
             $data[] = [$student->name, $student->present];
       }

       return $data;
}

This is model and I want to bring $request->year from controller in getAbsentDays function. So can i ?

protected $appends = ['present'];

public function getAbsentDays(){
     $year=$GET['year']; //for filtering 
}
 public function getPresentAttribute(){
          return $this->getTotalDays()-$this->getAbsentDays();
 }
automica's avatar

@coder72 what are you trying to apply the year filter to?

are you looking for TotalDays in a specific year minus AbsentDays in that year?

can you show me what you are the methods look like for 'getTotalDays' and 'getAbsentDays'?

Can also show me migration for table that contains year? or are you looking at filtering students by 'created_at' and year being part of that field?

coder72's avatar

@automica like this something when user select year. like 2018.Then Absent day and total days data will come of 2018 only.Suppose Database class have column name year and i want to filter it but how can i ?

public function getAbsentDays(){
     $year=$GET['year']; //for filtering 
     $class=Class::where('student_id',$this->id)->where('year',$year)->where('absent_days','1')->count();
}

public function getTotalDays(){
$year=$GET['year']; //for filtering 
     $class=Class::where('student_id',$this->id)->where('year',$year)->count();
}
automica's avatar

@coder72 I'd modify the methods in your Model to accept the $date

public function getPresentAttribute(): int // modified to get days present for current year
{
	$year = date("Y");
 	
	return $this->getPresentDaysForYear($year)
}

public function getPresentDaysForYear(int $year): int
{
    return $this->getTotalDays($year) - $this->getAbsentDays($year);
}

public function getAbsentDays(int $year){
    return Class::where('student_id', $this->id)->where('year',$year)->where('absent_days','1')->count();
}

public function getTotalDays(int $year){
    return Class::where('student_id', $this->id)->where('year',$year)->count();
}

and then pass in your $date as an argument

function attendancePerformance(Request $request)
{
    $year = $request->year;   //This is for filter where someone can choose year for filtering of absent year
    $students = Student::get();

    $data = [];

    $data[] = ["Students", "Present Day"];

    foreach ($students as $student) {
        $data[] = [$student->name, $student->getPresentDaysForYear($year)];
    }

    return $data;
}
1 like
coder72's avatar

What if i have many filter like year, subject and many more.I need to do this like given below ??

public function getTotalDays(int $year,$subject,$type){
    return Class::where('student_id', $this->id)->where('year',$year)->where('subject',$subject)->count();
}

Did i need to make individual attribute ?

automica's avatar

@coder72 just extend what you've got:

public function getTotalDays(array $filters)
{

    $query = class::where('student_id', $this->id);

    if (isset($filters['year'])) {
        $query = $query->where('year', $filters['year']);
    }

    if (isset($filters['subject'])) {
        $query = $query->where('subject', $filters['subject']);
    }
    
    // etc
    
    return $query->count();
}

Please or to participate in this conversation.