mardi 11 juillet 2017

how to use SQL Transaction in laravel 5.4?

i want to apply transaction in controller with multiple of model. where second model is dynamic.means more than 1 model will place into foreach loop.

 DB::transaction(function() use($request  ) {
 $user = User::$request->only('name','mobile','address');
 $userSave = User::create($user)->id;

 foreach ( $request->subjects as $items)
 {

  $data = User::$request->only('name');
   $data['student_id'] = $userSave
    $subject= Subject::create($data);

   }
  } //sql transaction end

I can not explain this better way .. so please read code and correct it. if i am wrong. problem is, it is not rollback if subject model query gone failed.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2sL2KEw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire