Babo
Babo

Reputation: 375

How to get data from foreach loop result

The data obtained from the $attendance function has four data. I want to loop through all the data I call on $attendance, but it only executes one data after I do the foreach loop. I am using the Laravel framework. How can I get data from the loop result?

$attendance = DB::table('attendance')
    ->where('company_id', $request->company_id)
    ->where(DB::raw("in_out"), "=", "in")
    ->where(DB::raw("(STR_TO_DATE(attendance_time,'%Y-%m-%d'))"), "=", $date)->get();

foreach ($attendance as $atd) {
    $employee_in_company = DB::table('employee_in_company')
        ->join('employee', 'employee.id', '=', 'employee_in_company.employee_id')
        ->where('employee_in_company.company_id', $atd->company_id)
        ->where(DB::raw("employee.id"), "!=", $atd->employee_id)
        ->select('employee.name as name_company', 'employee_in_company.company_id',
            'employee_in_company.employee_id')
        ->get();

    $employee_checkin[] = $employee_in_company;
}

return $employee_checkin;

Upvotes: 1

Views: 1319

Answers (1)

Atif Mahmood
Atif Mahmood

Reputation: 390

You should use arraypush function to get result.

 $attendance = DB::table('attendance')
             ->where('company_id', $request->company_id)
             ->where(DB::raw("in_out"),"=", "in")
             ->where(DB::raw("(STR_TO_DATE(attendance_time,'%Y-%m-%d'))"), "=", $date)->get();

$employee_checkin = [];
foreach($attendance as $atd){

                    // return $data[] = $atd->employee_id;
                  
                    $employee_in_company = DB::table('employee_in_company')
                    ->join('employee', 'employee.id', '=', 'employee_in_company.employee_id')
                    ->where('employee_in_company.company_id', $atd->company_id)
                    ->where(DB::raw("employee.id"),"!=", $atd->employee_id)
                    ->select('employee.name as name_company', 'employee_in_company.company_id', 
                             'employee_in_company.employee_id')
                    ->get();
                  
                    array_push($employee_checkin, $employee_in_company);
    
                }

                return $employee_checkin;

Upvotes: 1

Related Questions