mardi 28 avril 2020

laravel, mysql transaction not working once rollback

I am performing insertion in 4 tables simultaneously using laravel 5.8. When I Submit incomplete Form it will show me error and after that when submit proper data again it will show database error

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`matrimonial_db`.`personal_details`, CONSTRAINT `personal_details_ibfk_1` FOREIGN KEY (`site_user_id`) REFERENCES `siteuser` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) (SQL: insert into `personal_details` (`site_user_id`, `updated_at`, `created_at`) values (148, 2020-04-29 05:16:53, 2020-04-29 05:16:53))

First time when i fill form it insert data successfully in all tables but when try again it will show me the error. here is a code

try
        {
            $connection = DB::connection('mysql');
            $connection->beginTransaction();
            $userData = session()->get('registeruser_data');
            $userData->user_type = 'v';
            $userData->save();

            $personal_details= new PersonalDetail;
            $personal_details->site_user_id =$userData->id;
            $personal_details->save();

            $personal_bio= new PersonalBio;
            $personal_bio->site_user_id =$userData->id;
            $personal_bio->save();


            $partner_details= new PartnerDetail;
            $partner_details->site_user_id =$userData->id;
            $partner_details->save();

            $personal_img= new PersonalImage;
            $personal_img->site_user_id =$userData->id;
            $personal_img->save();

            $connection->commit();
            session(['siteuser' => $userData->id]);
            return redirect('/edit/profile');
            // dd($charge);

        }
        catch(Exception $ex)
        {
            $connection->rollBack();
            return redirect::back()->withErrors(['stripe'=>$ex->getMessage()]);
        }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire