Subscription flow example 0:00You'll find that database transactions can be enormously useful when building your projects. Let me give you an example of when you might reference one. Let's say, well, let's just have a resource for subscriptions, and I'll set up a subscriptions controller for that. And then finally, phpArtisan make a controller called subscriptionsController, and a plain stub is fine. Okay, so if we switch over to that, we're not going to really build this out. We're going to simulate it. So imagine that a user wants to register or sign up for a subscription to your site, very much like Laracasts.So imagine that a user wants to register or sign up for a subscription to your site, very much like Laracasts. Okay, well, if you think about it, a number of things need to take place. So for example, you need to create their account, of course, but then also, well, presumably they're paying you, so you need to bill them. And probably you're going to use some third-party provider, maybe something like Stripe to handle that. But now, well, let's see, if we were to just build this out, we create their account, we bill them, and then maybe finally we update their record to add some kind of billable ID.we create their account, we bill them, and then maybe finally we update their record to add some kind of billable ID. Okay, that's fine. But what if, yeah, we create their accounts, but we bill them and something goes wrong, and our third-party provider throws an exception? Okay, so now at this point, we have their account. We've tried to bill them, but that didn't go through. So what happens in these situations? They already have an account, so if we're not careful,So what happens in these situations? They already have an account, so if we're not careful, we end up in a situation where we didn't get any money from them, but we still created the account. And obviously, this is a very simple example, but you can imagine for larger things, where you need a way to say, well, I have to do these six different things, and if any one of these fail, I just want to roll everything back and let them know, Wrapping work in transactions 1:43and if any one of these fail, I just want to roll everything back and let them know, hey, you screwed up, you need to fix this. This is exactly what database transactions are for. And we can pull these in through Laravel's DB facade, and this will work for the Query Builder or Eloquent or anything like that. So we could just say, run everything within this closure in a database transaction. And the way it works is,run everything within this closure in a database transaction. And the way it works is, if at any point in this logic, an exception is thrown, that is Laravel's cue, oops, something went wrong, let's roll everything back. So now, and even here, if we wanted to build them first, for example, build the user using Stripe, and that will create a customer, create the customer. Okay, so we have that. And then finally, we need to create the actual local account on our website. Integrating billing and user creation 2:30Okay, so we have that. And then finally, we need to create the actual local account on our website. So create the new user on our website. And then of course, anything else that is paramount to this process. And let's see to make this a little less pseudocode. Maybe you have some kind of billable class or interface here. And now if we want to reference that, we could say, okay, billable, create customer or whatever you want to call it. And presumably you'd pass through some kind of token that you receive. That would be like an identifier for the user's credit card.And presumably you'd pass through some kind of token that you receive. That would be like an identifier for the user's credit card. Okay, so behind the scenes, you would reference your billable class. And that would take care of talking to Stripe, passing through the token and creating the customer. Okay, so we have that. Now we would actually create the user. So for example, use app user. And then down here, you could say user register, yada, yada. You get the basic idea.And then down here, you could say user register, yada, yada. You get the basic idea. And of course, you would probably add some kind of reference to the billing customer within your user record or within a has one relationship or something like that. Okay, so now, again, here's the important thing to understand. Assuming that we don't throw an exception, then this is perfectly fine. No issues here whatsoever. However, if during the process, for example,No issues here whatsoever. However, if during the process, for example, what if we try to bill the user, their card looked good, but when Stripe tried to run the card, it was declined, whether it appeared to be fraudulent, or maybe there's just not enough funds on the card. In those situations, it will throw an exception. And if we can't bill the user's card, then we definitely don't want to register the user for our site. So again, a transaction will pick up on that exception How rollback triggers 4:09then we definitely don't want to register the user for our site. So again, a transaction will pick up on that exception and instantly roll everything you did back here. And by the way, if you're curious how this works, well, we could visit the database connection class and there should be a transaction method here. Okay, so when we call this method and we pass through our callback, and remember, the callback is just every action or database manipulation you want to make. All right, so we begin a transaction. Transaction method internals 4:34or database manipulation you want to make. All right, so we begin a transaction. And all that does, if we take a look, is it increments this property called transactions and defers to PDO to begin the transaction. Okay, so now notice the try-catch here. This is how it picks up on the exception. So we will try to trigger this logic right here that I have highlighted. However, if an exception occurred, we immediately roll back. Something went wrong, so we're just going to roll everything back.However, if an exception occurred, we immediately roll back. Something went wrong, so we're just going to roll everything back. And if you take a look at that, notice that we reset the number of transactions back to zero and we ask PDO to roll everything back. Finally, right here, of course, it rethrows the exception so that within your controller here, you have the opportunity to catch the exception and respond accordingly. So for example, maybe you want to catch a billing failed exception and flash something to the screen and redirect back to the page. When to use transactions 5:26So for example, maybe you want to catch a billing failed exception and flash something to the screen and redirect back to the page. You can still do that. All right, so that's all there is to it. It's not for everything. Don't feel like you have to run a transaction for every single database operation. Think of it instead as if you have these series of modifications that must take place. And if you decide that, well, if any one of these doesn't work, I want to roll everything back. That's exactly when you need a transaction.I want to roll everything back. That's exactly when you need a transaction.