1

I am getting this kind of data from the database in array form.

Illuminate\Support\Collection {#1434
  #items: array:4 [
    0 => 20
    1 => 21
    2 => 22
    3 => 19
  ]
}

I want to insert the this array data through the controller For each array element.

 foreach($plucked as $data){
    $attendant_data = new EmployeeAttendant();
    $attendant_data->user_id = $data;
    $attendant_data->date = Carbon::now()->format('Y-m-d');
    $attendant_data->time = Carbon::now()->format('H:i:s');
    $attendant_data->present = '0';
    $attendant_data->save();
 }

Data is being inserted only for the first array element.

1 Answer 1

1

Just prepare the data and insert it.

$insertableAttendant = [];
foreach($plucked as $data){
    $attendant_data = [];
    $attendant_data['user_id'] = $data;
    $attendant_data['date'] = Carbon::now()->format('Y-m-d');
    $attendant_data['time'] = Carbon::now()->format('H:i:s');
    $attendant_data['present'] = '0';
    $insertableAttendant[] = $attendant_data;
 }
EmployeeAttendant::insert($insertableAttendant);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.