Laravel E-Commerce - Orders in Database - Part 18

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys welcome back and in this video I'd like to start using an orders table in the database for our application up to this point we've just been storing the order as metadata and retrieving this info within the stripe backend I'd like to store this info in our own database which will make our application more robust for example we'd be able to check orders from within the admin and later on build out our user dashboard where users can check their past orders so I'm gonna use two new database tables to accomplish this one I'm gonna be using an orders table which stores all the information about the order it's also gonna have a corresponding order model and there will be a one to many relationship between users and orders I'm also going to have an order product pivot table which will store all the products for the Associated order this will be a many to many relationship between orders and products so let's go ahead and start so PHP artists in make model order and let's make a migration for that let's go into our editor let's go into let's set up the relationships so it's going to the model and that's just to a new method here let's call it users sorry user and it's just returned this belongs to app user and let's do the same for our user model so down here we can specify a new method let's call it orders and it's returned this has many order okay now let's go into our migration so create orders table and set up a few things here so first thing I want to do is set up the foreign key on the users table so we want to store user information on this table so we'll do a table integer user ID it's gonna be unsigned it's gonna be inaudible and the foreign key is gonna be table foreign user ID the reference the ID on users table and I'm gonna do on update cascade and on delete I'm gonna set null okay and for the rest of them it's just gonna be all the order information except the products so for example we'll have sorry that's strong string billing email so basically all this information here let me just put something my card I have to cart checkout so okay so this user ID field is gonna be null if it's a guest and it will have the ID if we're logged in so we'll do guest so yeah the order information just gonna be everything here so let's go and actually I'm not gonna make you watch me do that it's gonna paste that in okay so I pasted everything in this should be table sorry and yep this is just all the information including the totals including the discount and I added a few more fields here payment gateways you're storing if it's stripe and maybe later on I'm gonna do PayPal shipped is just a boolean that like has a status on the order saying if it's shipped or not and by default it's not and an error is it's gonna be storing if there's an error here because sometimes an Eric can come back from stripe so if it's no then there's no error but if it has an error message will store it in here as a string okay now let's make a pivot table I'm gonna want a model for that as well so let's make a model PHP artisan make model let's call it order product and it's making migration for that as well okay so since this is a pivot table we don't want this to be plural so let's change that rename to create order product table it's also make sure we change this and this and this too so in our migration here all we're going to do is store the order ID as well as any product that's associated with this order so let's just do something similar here table integer order ID this is gonna be unsigned and it's gonna be nullable and we're gonna have a foreign key table foreign order ID references ID on orders and on update let's cascade and on delete that's said no so I'm going to do the same thing for the product ID so let's paste that in let me just replace them product and I also want to store the quantity here so let's just do an integer for quantity and let's make it unsigned okay let me just go to my model a real quick and make sure we specify the table here so protected table is order product and we also have to set the fillable here protected fillable oh sorry yes should be an array order ID product ID and quantity and we also have to do that on the order model just gonna paste that in oh yeah and we also have to set the many to many relationship between orders and products so we can go ahead and do that in here let's do another method called products and this is gonna return this belongs to many app product and I'm also going to do this with pivot because we also want to access the quantity we'll use that later on and I'll show you when we do use it okay let's go ahead and migrate this PHP artisan migrate ok now let's put this to use and whenever a checkout is complete we want this to go into our orders table so what's going to check out controller and right where is it right here is when it's successful so right here we want to do two things we want to insert into orders table and then we want to insert into the pivot table or the product table so let's go and do that so order is order create and make sure I import this and the first thing is the user ID and like I said if we're logged in so if auth user then we want to store the or that logged in users ID so off user ID and if not then we'll just or no and for the rest I'm just gonna paste them in so you don't have to watch me do that okay so I pasted the rest of the fields here it's just coming in from the request these numbers we were already using before we have this this numbers helper which gets all of these like totals but I've added one more thing since we want the coupon code I didn't have that before in this method so I just added that here and we're returning it here so we can grab it here for the code and this error should be sorry no because there's no error in this case but later on we also have to hand handle this case down here where there is an error and we'll set that error to whatever is coming in over here we'll extract to a helper in a second all right now that store everything in the pivot table so we also have each product for the order okay there should be a semicolon here alright so be for each I'm back on V as cold as you see but these snippets have been really finicky that's why I went back to sublime looks like you're still issues but let's go ahead and do this so for each others this is gonna be cart content so each of the cart items as item let's do this so let's use our order product model and let's create sorry create we just have to pass in the order ID so it's going to be this order up here or ID and the product ID it's just gonna be the item model ID and then the quantity is just item QT Y and it's make sure we import this cool and make sure we have semicolon and if we did this correctly this should store our order in the tables that we created so let's go ahead and give it a test I'm gonna put one more item in this so we can see that pivot table working so let's do one more let's do desktop one I have to cart your seat to checkout okay I'm gonna log in for this one and then we'll also try the case where we're not logged in I think I have an account I guess not okay we'll just check let's make what actually user password password okay so now I'm logged in there's the email address and let me just quickly fill this over and we're going to do the positive case here so a successful credit card and we'll test out the error as well after okay so hopefully this works and it did so let's check out our database let's refresh this and there should be stuff in here and there is as you can see the user ID is populated and here is all of our information there's no discount code so it's no and here are the total stored and cents and there's no error and it looks good let's check out the order product pivot table so yet we had two products in here we had this and this and those are the associated quantities awesome looks like everything is working okay let's try the case where we're not logged in so let me log out yeah so yeah it's two phone for Add to Cart let's put one more in it's 2005 at the cart proceed to checkout so let's test the guest login one and I'm also going to test the discount so let me just fill this in also make sure it went it is stripe so let me just check the payments okay there's that one there yeah this is the one we just did cool back here so I'm testing two things here the guest login and the discount code actually lets to discount code first let's do I think I have one called abc123 does that not work okay I know I redirected there okay so this is a thirty dollar discount and let's see if it stores the discount and the coupon codes let me fill this out again so again positive case oh sorry and that worked let's check out our database refresh there's those two new orders with this order ID and this time there is a discount and a discount code being passed through and it looks good all right so let's focus on yeah let's extract to a helper method here so for all of this stuff I want to replace it with a helper method because we're gonna need this again in the error case over here so let's just do this let's call it add two orders tables and we have to pass in the request and we have to pass in if there's an error or not so in this case there is no error so a person though and it's cold over here and make a new method so let's still protect it I'll add two orders tables passing the request and the error and paste that in and I'm going to change this to error and everything else is the same okay but now we can also call this in the error case right here and set this to this over here and that should store the error message in our database so yeah let me just do two tests let's just make sure that it still works so I'll just do one now so you check out I just do have a guest and I okay so the test credit card for an invalid credit card is this number and let's see if this works okay we get the error let's see if that's successfully stored in our database and it should be this one right yeah and there is the error right there the same error that's printed to our screen and it's still stored in here but we have it as an error in our main orders table all right now that we have our database in place I would like to put the orders inside of our admin dashboard here in Voyager so let's go ahead and do that first thing I'm gonna do is make bread for orders so let's add bread to this table and I'm gonna just fill this out quickly all right so I filled all of these fields out so let's submit this and down here we have our new orders menu item let me just move it up should be admin we want this to be first because it's pretty important in our application and I forgot to give that a icon so let's quickly put give it an icon void your documentation right here okay actually no it you change the icon in here I think yeah Voyageur documentation there you go okay so now if you click into orders you'll see all of our orders here so what I want to do is show The Associated products with these orders because as we've done there's a pivot table associated with each order so it's the interview let's put it within the view view so right here I want like all the products here so we're gonna have to override some controllers and some views now if you watch my other video on customizing Voyager you'll be familiar with this already so back to orders let's edit this I'm gonna need a custom controller so we need this we need to specify this in our code I also make all orders controller so let's save that and now if you go to orders that should be an error because that controller doesn't exist and let's go ahead and make it so then here let's just make a new one called orders controller HP and let me just paste that from the other one actually no let me just do this paste it from here okay so the only method I want to override is the show method and the show method corresponds to the view so let me just go ahead and grab that show method HTTP controllers void your bread controller right so it's just show this method right here let's grab this copy this let's go to orders controller let's paste that in and this should not work okay cool we also want to override the view so let's grab that here as well red so we want to override this read yeah it's the read one all right back into our code right here and sorry are you resources views here we go vendor let's make a new one called orders new file read top blade top PHP paste that in and let me just see if that's the correct one review okay cool refresh okay so we have that in place so here in the controller I just want to grab the order here so order is order fine by ID and I want to get the petite products associated with that order so products is order products and it's pass that through so right here this is specify products and this should still work okay but we have that products variable being passed in now so now in our view so right after the panel body right here we can just do something like this so let's just make an unordered list again vs Co doesn't like me come on Amit okay and it still for each products as product let's let me just stylus margin-bottom ten pixels and for each of these we just want to have the product information so product ID product ID and a few for the other ones so this one will be product name and that will just be product name and the next one of the product price product present price and quantity here and we can just do product pivot quantity and that's why I did that with pivot right here so we can grab the quantity straight from the product and yeah that should do it see if it appears here and it does oh sorry shouldn't be within the ID let me just grab this yeah sorry I should have made its own section here let's go and do that so let me just duplicate this sorry duplicate this and then let's make a panel body as well just duplicate this and now I can put in here and that should do it but we'll name this sorry though this should be outside grab that that should be back to normal okay and there should be outside the for each right here okay no yeah so we just name this products for this order okay sorry about that there we go let's take a look at another one that has multiple products so go back to orders not sure which ones which there you go that's nice too and you can do more with this I would definitely encourage you to like make it look nicer and maybe add the images but all the information we need is here okay the next thing I'm gonna do is color code these rows when in mint comes in I want to be able to quickly glance and see what needs attention so for example I'd like the error row to be red and for a row that's been successfully shipped already I want that to be green and otherwise we can just leave it as white so we're gonna have to override the Browse view so let's go ahead do that so let me just grab the Browse view from Voyager so right here let me grab this and let's make our own right here orders new file browse top blade PHP and actually let me make sure that works should be blank okay cool so right here data type content right here this is where you can set a style so let me just do background read see if that changes the color okay cool so that's where we have to specify the classes based on our data so here's what we're gonna do we're gonna do class equals sorry okay let's do a ternary here so data is just our model so we have something called error right we specified that in our order model so if data if it has an error then we want to append the class under to danger and if not do nothing so this one has an error already so only this one should be read see if that works it highlighted that but it didn't highlight the yeah how come okay here's what we're gonna do we're just gonna define our own classes because there's some specificity weird errors going on so let's just do this instead of this class which is a bootstrap class let's just make our own let's call it order error and we go up here we can actually specify a section here called CSS and it's just stop it there and that's just the style and it's just do order error and let me just grab this stuff here and just put in here just for reference so order error would be this and we want another one called order shift sorry there should be a class and that should be this stuff okay don't need this anymore and let's see if that works it still does not work why not the background color is still more specific in this class will help if I do this I don't think it will okay it does cool that's fine for me okay so let's do the case where is it yeah let's also do a case for if it's shipped so let me just grab all this we can repeat this right here within the quotes and let's just do order sorry data we can do order shipped okay that's not going to show because we don't have any orders that are shipped but the idea here behind that shipped field is we get an order and we do whatever we have to do and we ship it off and once it shipped we go in here we can edit it just close this and we can change this like to shipped you can save that and hopefully that's green and it is so we just have a quick glance of whether or not our orders are shipped and what we still have to work on and as you can see here the dollars are still in cents but I've already done that in my other video so just go ahead and refer to that if you want to change this to sorry I want change cents to dollars and also within here I'm gonna do that behind the scenes but like I said just check out the other video if you want to do that yourself so all the dollar amounts here cool so there you have it guys we've added orders into our own database so we can view them from within the Voyager admin dashboard I'll make sure to update the installation script to include all of these order fields that we added in this video please like comment and subscribe if you haven't already done so thanks for watching guys see you in the next one okay thanks bye [Music]
Info
Channel: Andre Madarang
Views: 24,706
Rating: undefined out of 5
Keywords: laravel ecommerce, laravel e-commerce, ecommerce laravel, e-commerce laravel, drehimself, andre madarang, laravel voyager, voyager laravel, laravel admin, admin laravel, laravel crud, crud laravel, laravel admin builder, laravel crud builder, laravel orders database, laravel orders table, orders table, laravel persist orders, orders table laravel, orders database laravel
Id: 0lo7vzO1Fto
Channel Id: undefined
Length: 38min 3sec (2283 seconds)
Published: Mon Feb 26 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.