search

Home  >  Q&A  >  body text

No active transactions'

wipe_dataThere is a problem with this function

This wipe_data Function my database cleans and manages data insertion But this function shows error:

No active transactions

This is my code:

function wipe_data() {
     DB::beginTransaction();
     $adminData = User::where('role', 'admin')->first();  
    try {
        User::truncate();
        User_details::truncate();
        User_kyc::truncate();
        Token::truncate();`enter code here`
        $auto_id = date('Y');
        DB::statement("ALTER TABLE ls_users AUTO_INCREMENT = $auto_id");
        $admin = new User();
        $admin->username = $adminData->username;
        $admin->email = $adminData->email;
        $admin->password = $adminData->password;
        $admin->role = $adminData->role;
        $admin->save();
        $user_id = User::where('role', 'admin')->value('id');
        DB::commit();
    } catch (\Exception $ex) {
        DB::rollback();
        return false;
    }
    return true;
}

P粉242741921P粉242741921339 days ago494

reply all(1)I'll reply

  • P粉680087550

    P粉6800875502024-02-22 00:31:52

    There are some statements that cause an implicit commit , including the ALTER TABLE statement you are using.

    So, before calling DB::commit(), your statement has already been committed, so the error occurred.

    reply
    0
  • Cancelreply