jeudi 18 juillet 2019

How to fix rollback transaction in laravel when used try catch in 2 model?

My rollback transaction doesn't work in my controller, when I used try catch in model? Here in My Code

try{

            DB::begin Transaction();
                $array = array("0" => "1", "1" => "2");
                for each($array as $data){
                    $data = new Test();
                    $save = $data->test();

                    $data = new Second();
                    $save = $data->test();
                }

            DB::commit();
        }catch($e){
            DB::rollback();
        }     

Here in My Class

public function test(){
    try{
        $log = new Test;
        $log->id = '20';
        $log->text = '30';
        $log->save();
    } catch (\Exception $e) {

    }
}


public function second(){
    try{
        $log = new Second;
        $log->ids = '40';
        $log->text = '50';
        $log->save();
    } catch (\Exception $e) {

    }
}

how make it rollback works with my code



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire