vendredi 10 mai 2019

Laravel dry run on eloquent save

Is there any way to execute a dry run in Laravel when saving?

Lets say I have 2 models, Booking and BookingDetails. I do not want either one of them to save without the other.

At the moment i'm filling an saving like this

//Create new booking
  try {
       $booking = with(new Booking)->fill(request()->all())->save();
       $bookingDetails = with(new BookingDetails)->fill(request()->all())->save();
  }
  catch(\Exception $e){
       //Die quietly
  }

Ideally I want to be able to check the booking save is going to work first, because if the booking save doesn't work, I don't want to save the details. Then if the details can't save, I don't want to save the booking in the first place, I just want to throw an error and save nothing if either fail.

Is there any way I can dry run and test all of the saves work, and if they do and they're all OK, save for real? Instead of doing something like this

//Create new booking
  try {
       $booking = with(new Booking)->fill(request()->all())->save();
       try {
         $bookingDetails = with(new BookingDetails)->fill(request()->all())->save();
       } catch(\Exception $ex) {
          //Find the booking here and delete, because the details cant be saved
       }
  }
  catch(\Exception $e){
       //Die quietly
  }



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2HepUgu
via IFTTT

Aucun commentaire:

Enregistrer un commentaire