erhsaikhasif's avatar

How to insert bunch of records in table, using transaction complete or rollback concept?

Hello, I have to insert the multiple records in student table using post method. I will send this student entry in json array. But the insertion process must be either insert all or none. There is a concept in SQL either transaction all or none or rollback. How can I achieve this? If you have alternative solution for this then please suggest me. I tried this by using foreach loop , but their is no guarantee of insertion of all records. Please help me. Thanks.

0 likes
3 replies
Francismori7's avatar
DB::transaction(function() use ($studentsArray) {
    foreach($studentsArray as $student) {
        Student::create($student);
    }
});
2 likes
erhsaikhasif's avatar

Ricardo Thanks for the reference it was helpful for me. Thanks Francismori17 too and I appreciate ur help too because ur given example helps me more.

Please or to participate in this conversation.