1
votes

I am trying to add attendances to the attendance table and if attendance of the give date and student id is already in the table I need to update the attendance type.

But the upsert is only inserting and it does not ever update.

SmStudentAttendance::upsert($studentRows, ['student_id', 'attendance_date'], ['attendance_type']);
2

2 Answers

0
votes

upsert works exactly like

on duplicate key update on MySQL

which needs a unique constraint violation to update instead of an insert.

So check if you have such a violation, directly in the database.

Also you can read more about with an example for example at https://www.amitmerchant.com/insert-or-update-multiple-records-using-upsert-in-laravel8/

0
votes

It's too late but it worked for me. Try that in miggrations. $table->unique(['student_id', 'attendance_date']); or you can check both of column an choice unique in myphpadmin