Building a shopping cart app with Laravel, Vue + Stripe

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
please hey what's up everybody all right so today i'm gonna try to build a review applicator a larva application uh with a view front end and we're gonna use laravel cashier in order to be able to make purchases it's basically gonna be a really simple uh shopping cart kind of application um the goal for this is that this is going to be the source code and kind of the overview of my next youtube video so you'll kind of see the process that i go through for each of these videos where i kind of just work through and build a test app and um see what comes out of it all right so i got php storm open with that which has nothing in it so i'm going to go ahead and get my dev environment setup with docker it's not empty it's in here okay i don't know if the music's too loud or anything i'm going to use view inside of laravel so it's not going to be a separate view app purely because the bulk of this work is going to be in laravel um so it's going to work like a single page application but instead of it being like a separate um view app and a separate larval app i'm just going to have a single blade template that's going to render out a view app and then that view app will act like a single page application but everything that we're going to be that i'm going to be doing with this application you should be able to split out into two separate components because all of the back end functionality and everything is going to be wrapped under the api routes and anything that we do is probably going to use sanctum in order to run authentication and handle the cookie software and everything uh hey what's up robert good morning from australia um i did check out laravel sale um it's pretty nice actually um i'm just i'm really happy that they uh got docker into the official larval repo uh i know a lot of people are excited about that and i'm happy that it's getting more of the spotlight and uh someone from twitch is saying inertia uh i haven't used inertia actually um i've wanted to check it out never got around to it um i'll probably put that on the old 100 items to-do lists and check that out eventually okay i got my get repo cloned in here so now i'm just going to boot up everything with docker hope it's not called nginx what is it called is it site yeah okay cool okay and i've been in so larva project laravel larval and here oh what's going on now failed to execute script docker compose i've never seen this error before the joys of working on windows for the first time in a long time run composer no such file or directory the hell is this ah it's always fun now let's find out what's what's going on here fail to execute script docker compose let's see what stack overflow has to say about this make sure docker for desktop is running it is i literally just brought up docker with uh the script see docker ps that's working fine docker compose ps but docker composes nuts that's weird i don't know why there's something wrong with my compose script maybe someone's saying this has to be a double quotes maybe that's what it is let's try that out if that is then i have to make an update to that repo nope nope no such thing as dock or compose let's see what else we got here fail to execute script docker compose just updated docker too so i wanted this is a bug default stack orchestrator regular does docker compose even work if i do like docker compose v yeah documents is working ops not even working anymore what is going on here this is so weird watch me go through all of these uh this lovely debugging process try to delete them and download them again all right well let's let's try to kill the docker process and bring everything and bring everything down it's not even running down jesus let's stalker compose kill talker compose kill doesn't even work reinstalling docker reinstall dagger dock compose version mutual match local docker compose install oops really b there we go it was just it was just working yesterday too yeah let me just try to just bring everything down open up docker desktop bring everything down this way i was having an issue with this earlier let's see if we can stop these containers and destroy it running let me try to bring docker back up and down again and just exit it completely i know there's been an issue too because i'm using wsl2 with the docker docker compose all right so the wsl backend has been stopped let's bring this back up again noseclam tony said update my wsl it should be on the latest one i'm using the nightly builds on windows 10. um so we should have pulled in uh the latest stuff from last night this afternoon but i guess we'll find out all right and this is up and running again no containers cool so now let's open this up and try again i see it's running the build process just fine that's what's really throwing me with this oh yeah wsl is getting nice to the best of both worlds but there's still so much that uh you just can't get from a real unix uh system all right let's specify the file name all right doc compose f docker compose.yml run arm composer i know it doesn't work either run docker ps shows the containers up and running for some reason just it doesn't want to run anything besides up this is so weird come to my stream where you can watch me debug docker issues on windows for the next 30 minutes here maybe if i specify that i'm doing this on wsl i'll look at that wsl docker compose up now it's ups not failing up is not failing at all fail to execute script docker compose let's try this reddit always gives me good advice no package isn't installed what lock file this isn't a problem orlando version operating system ssh into the nginx container and see if composer is in there well composer should be running on its own container um but i wonder i wonder if it'll let me even execute that um let's go into the engine x container bin sh nope this is so strange um does he even let me run yeah so it lets me it shows me the help if i just use run but for some reason specifying something for run doesn't work doc compose we're on maybe with a heart out rm composer nope i feel like something is wrong with this docker compose script that's causing this issue let me specify the version is 3.8 which is fairly new because we have that composer file here right here you can build without sight is it because i didn't build the composer that makes a lot of sense oh boy oh wait hold on i know my doctor composed bill just fails to but here let's try dr compose up build that's weird that's not working here even though the containers were built before let's try stopping these again and removing this okay so docker ps shows nothing to compose up the build without specifying the site name throws an error but if i specify site it doesn't i get what is going on you all saw this is working just fine with dr compose up like five minutes ago something's definitely a miss here what'd you send me nose clam tony compose issue let's see we got here fails in wsl environment so any resolution for this same is with mac before in store creds removing the file worked for me in dot docker config json tried removing the docker config json come here the store creds and there's a lot to unpack here that's right how it says to remove this file and then let's try rebooting our docker and see what happens here i might have to reboot um my command line as well because wsl doesn't like working with docker when i start it up first back up back up all right let's see if this works okay that's back working here up the build and we won't specify site this time and it fails let's try reopening up wsl i actually might just say screw it and use uh powershell for this as i know that i'll probably work on that sites so we can get some development actually working today all right so this is building everything everything's built i don't have composer locally on this run rm composer yeah let me try let's try opening up a windows powershell and seeing if this works city sites rv view stripe tests clear goes run this is actually working though so yeah i think i'm just going to use powershell for now until i can figure out what's going on with wsl so let's at least get our larval project up and running oh yeah i forgot there's a readme file in there let's clear that out composer 2 is just so much faster on on docker than it used to be i mean this process itself just getting larval uh like a plain larval project installed uh using docker and composer would take easily three to five minutes and my fans would be worrying the entire time and now this thing's gonna take probably under a minute and this is you know all the dependencies and everything from composer has just so much faster i hope you're all doing good this saturday night or sunday morning depending on where you're at uh we started about 20 minutes ago yeah composer 2 is insanely fast ah yeah well this finishes up um so first thing i'm going to start doing is getting some migrations up and some models up for this basically what this application is going to have is we're going to do probably a product uh [Music] and an order model and table because essentially the user is going to go onto the website they're going to see a variety of products maybe do a categories table too a price for each of these products maybe an image description a title and then they'll be able to click on one read about it it'll go to an individual page for it on view um and then add it to their cart that cart and the items that are in their cart are going to be stored through vuex through the state management library and then on checkout we are going to use view uh sorry we're going to use laravel cashier and tie this to stripe so stripe will be handling all the web payments we should just be able to pass in uh some basic information user's email address credit card information and be able to check out and send them to some kind of success screen how long will this stream be i'm not sure um they'll definitely be a break in between it i'll probably be running i'll probably be on here right now for maybe like another more another hour or so and then i'll probably take about an hour or so break to get some food and boot it back up later if you guys are interested to see how far we get right now all right looks like our view application is up and running are alarm applications up and running we got everything in our source directory let's just gotta change out some env files or values so we gotta set this to my sequel for the hosts homestead homestead and secret and we're using uh was it larval tests so now if i go back here this should this i can be reached why can't it be reached i'm not running on port 80 by default and what am i on 8080. let's bring that back up so it's on the right port is php storm speedy um it really depends on two things uh depends on your system and the complexity of your project so um right now i'm on a pretty powerful pc so it's really speedy for me um searching through files or doing anything um in it like booting it up it's really fast but um so for portability i have a macbook air that i use uh just the i3 model and it's it's kind of slow on that i actually use vs code for that a lot for that system a lot because it's just faster but yeah if you have um decent hardware it's it's it's well worth it i mean everything just ties into this ide so nicely all right is our site up now let's find out my canvas why can't it be reach what's going on here it's up 480. if you reach a local host did i not okay welcome to richard localhost i don't know why my oh i never changed my hosts file like i thought i did i'm gonna go ahead and do that over here in the background what does delegated in volumes mean for docker when you have a delegated volume it essentially tells docker and the virtual file system that runs between docker and the containers and your local system it basically tells it that the source of truth for the data that's in that volume should be in docker so if for any reason you're doing a lot of writes to your file system in a volume between docker and your local file system if they're running kind of fast together docker is going to ignore writes that are coming from your file system versus the ones that are coming from inside the container itself um and it usually lends it to have like a speedier load time for it but the caveat with that is that if you are doing a lot of writes from your local file system on your computer they might get overwritten um for most of what i do i use delegated because i don't see any issues with that i've never had my files ever written because i don't do a lot of writes for my my local system and it makes load times a decent amount faster on um on some docker setups i hate finding the host file on windows it's so obnoxious um okay now we should see this updated beautiful beautiful yes i have heard about larval sale i'm really excited for it i tried it out um this week and uh i like that it's a one line command if all you have to do is get docker or desktop installed for your system or the docker repositories if you're using linux and it's a single command line for any system to get a local laravel dev environment set up i think it's it's good because in my opinion it lowers a barrier of entry for developers who don't want to install requirements on their install software on their local system php mysql it's awful to set up it breaks every update at least with my experience so having something that's in the official documentation that's super easy to get set up with this it just opens the world to um more novice developers and people who don't like working with you know the system side of things and the uh the hardware side of things alright so let's get those migrations added in so i definitely talked about um product i talked about category and order i think it's what we'll start with now if we need to add more later we can and so i'm going to use docker compose to run these um with artisan and we're going to say make model product and throw in the migration flag which will create our migration first as well yeah i'm drinking coffee right now too it'll keep me up through this all right order and category i think that's all we'll need at least for right now so each product is going to have uh multiple orders associated with it and each order will have multiple products associated with it so we're looking for like a many-to-many relationship there each product is going to have multiple categories associated with it and categories with products so another many to many relationship and then we'll probably will probably create customers in the users table since that's already there and i'm not going to have like any kind of dashboard or anything with this so we'll just dump every order that happens we'll just dump them into the users table and so we'll put a each order we'll have a user associated with it and once a many relationship one user can have multiple orders okay let's get into our php storm and start scoping out these migrations so let's take a look at our default users table first i don't really need an email verified at uh email password that looks good i don't know if we're going to be storing a password for these people so far but just keep that in there all right products so we need a name for the product so we do string name uh we'll do an about so we'll do text description you set uid4 by default new tables created i don't i don't use uuids at least um at least like in any kind of personal projects or anything like that i have used them in the past um i like euids for ids purely because um especially if you're doing stuff with a user facing side of things like an admin dashboard or anything anything where your end user is going to see some sort of id it looks cleaner to have something that's not sequentially added because uh then you run into the case where people could see like a really low id and think oh your application's not you know that popular as much as you'd like it to be so just that unique string value of a uuid is is better in that regard if you're using docker for db purposes takeout is very nice yes i've tried that out when it was released a couple of weeks ago or a couple months ago i don't know 2020's time is like just gone out the window um but yeah titan creates some nice products i really like that they uh they came out with uh take out all right let's see we got name description um for the sake of simplicity i'm not going to get an image i don't feel like dealing with image uploads and stuff like that right now so we'll keep an image out of here uh price we'll need a price we can store that as an integer default zero dollars um it'll be stored as cents so then on the front we can just divide by a hundred and put a decimal place where it needs to be to get an actual dollars representation that's kind of how i like sewing prices i don't like storing prices as as decimals off the bat it also makes converting to different currencies a lot easier at least i found i think that's it not gonna do any variations not gonna slug slug is good that is really good put that up here we'll need that as an identifier and i'll probably use a um storing has a double yeah that's also oh my light just went out there we go storing in a stubble would also be good um i just i hate dealing with decimal places um with prices but it's personal preference um if this was an enterprise application i probably would go double unique for slug also good nice okay this is looking good here sku for similar products well let's keep the slug for now um you know this is literally just going to be a super simple example kind of uh shopping cart application but yeah if this is uh any kind of public facing and we need a sku we need a product id upc some kind of identifier um variations images obviously orders so for the order we're going to need um i think we're going to need some kind of id from stripe i don't know what it's called uh i think they call it like a checkout id maybe check out token i'll have to edit that later um we can store the total of everything as well as another integer for price we set a default to zero um we need the user id because these are going to be attached in a one to many and this looks good for right now um i don't think we're gonna have to have anything else on here just to show because i mean the bulk of what's in the orders like page is going to be products which each of those is going to be in a mini mini table which we'll need to create the migration for that as well and lots of categories so for categories super simple we'll just do a title actually we'll do that as name to match the product table and like the productive we'll do a unique slide as well that's all we need for categories transaction id that's use that that sounds better than stripe id perfect okay now we need to create that intermediary table okay so for many too many relationships on larva i'm trying to remember it's the it's alphabetical order underscore no plurals so we are attaching uh products to orders cdfghj pq so o comes first and yes i have to do that every time so it'd be create order product table and that should populate by itself you used to have to throw in the table um flag or option and in order to pass and what you want the table name to be but i don't think you need to do that anymore i think it'll automatically generate that for us it does so it creates this order product table and so we can get rid of everything that's in here and just table integer order id and table integer product id okay and then we need one for the products and categories so we'll need to create category product table and open this up remove this table category id table product id okay so now we have our migrations up and running and we can run those by doing dot propose run rm artisan migrate and it should populate the table the database table let's find out it did okay so let's open up those migrate or let's open up those models and start scoping something out and um if you haven't played with larval yet it um quantity for order product ah that was that's that's a good one let's create a new migration then since i already migrated everything let's add quantity to order product table and we'll specify the table here for the product no rollbacks here just straight ahead it's a table uh integer quantity default 1 after product id and then table drop column quantity for the down and let's run the migrations hey look at that okay now we have everything that we need up and running so we can open up our product model and guard is just going to be empty right in all these i don't i don't care um that items get added in willy-nilly we're using this as an example so it's really not going to matter that much and then uh this is attached to a user so public function user is returning one or no that has one belongs to user class and then each one of these has i feel sneeze coming on multiple orders attached to it this as it belongs too many i think it's belongs to many six odd years into larval development and i will never remember relationship classes that much i go to the documentation all the time belongs to category yes public function category category i think that looks good for our product order and see this is this is something that i really like about using phpstorm is you can see as i'm in here if i just type in protect it it'll show me all the attributes that i could expect on this model and it just um that's what makes it worth it it saves so much development time product doesn't belong to user does it no wait yeah no it shouldn't belong to a user how do i put that there my coffee's not kicking in yet orders belong to user so each user can have multiple orders but each order only belongs to one user products each order has multiple products and i believe that's it for the order model product okay yeah each product has has a category and has multiple orders and the user change this regarded remove we're removing this email verified up because we're not even using that so but it has each user has multiple orders associated with them we return this belongs to nope as many border class if i could hit the right buttons that'd be great a user can't have multiple products through order um that is right but i'll probably just keep that out for this i guess simplicity from this back end and then did we do category we did not do category uh i think that's all we need it's just categories belong to products and products belong to categories and there should not be counter speed categories okay that's looking good so now what i'm going to do is i think i'm going to work on getting belongs to many in products to category yeah i got oh blog so many huh are you guys going to watch a lot of mistakes on this i can guarantee you thank you so much to the chat for helping me out through this because this is not an uncommon occurrence okay um next step is i'm probably going to use factories and cedars so we can get some fake data in there up and running um so i don't have to go into the database manually or so i don't have to use tinker or anything like that to get a lot of uh test products and then after that we'll build out our api and i will show you the products as they come through so let's get started on that so database factories we already have our user factory here and basically what that does is that it's each factory defines a fake complete model of that table or item so each time that we run this factory it's going to return back to us a complete user model here with all the attributes that we're expecting um pass through we're actually getting rid of the email verify because we don't need that so the fakerclass um which comes installed in larval by default is going to generate a name for us it's going to generate a unique safe email for us and it's going to create this password hash which resolves to just password and it's also going to create a random token for our remember token since we're not going to use that any of the seeders or anything like that it just doesn't matter but the database needs it don't know if it needs it but it's gonna have it in there so now we're going to create one of these for each of our models that we want and then we're going to then create cedars to create the actual models that get saved into the database so seeders use factories to generate the data responsible and then save the data into the database factors are usually generally used during testing they're used to create like fake models so that when your tests run you can test them against what the data is that you're expecting to come out of the database without actually having to create save that into the database but you could also use them if you need to rapidly seed your tables with data for any reason uh yeah this so this video is going to be um not only saved on like twitch and youtube to view again if you'd like to but the entire source code for this video and the whole general uh gist of this of this stream right now is going to be in a video that i'm releasing next week uh about creating an application uh creating a simple shopping cart application with laravel view and stripe so if you'd like to find if you'd like to see that you'll be able to check that out on my youtube in a few days and the length for that will be much shorter than this stream it'll be condensed down to probably about 30 to 40 minutes okay so we have our user factory that looks good now to generate factories for any other class or any other model all we have to do is use artisan i can use that compose artisan make factory and i believe let me just hop over to the documentation and see what the actual factory relationships defining model factories make factory okay you pass in a model flag with the model name so make factory model equals oh i need the name first product factory model equals product did i do something wrong oh i used three flags for the rm wow whoops it's time i get new glasses all right that's done we'll create another one for our orders which might be kind of hard to do but the most difficult part of this seed is going to be getting those relationships added in but we'll wing it when we get there and the documentation i'll have to read through because i haven't done relationships in the factory in a little bit so i'll go through and get re-familiarized with it with you guys why do i use shortcuts to retrieve the last command you wrote because that would be too easy honestly i don't have an excuse for that i have um so on my uh daily driver computer uh on my command line i have all types of aliases i just haven't had the chance to put them into this windows machine yet i got this set up for development not too long ago so i usually have commands for duck bows up down run um which is usually dcr if you see that any of my videos dcr is just a shortcut for docker compose run with the rm flag um just haven't have another chance to get those set up all right so we got our factories up here ready to go oops so see we're using this factory here in extends factory which allows us to then implement this faker before larval you had to implement the faker class by itself but now it's included in all these factory classes so that makes life a lot easier so we have a product factory here and our model is the product class and so we need to return a set of attributes which are required for this model so we're going to open up go to user factory we're going to open up our migration for the product table at the same time and just match what we have here so we need to create a fake name first and let's like to go with it so we can return name this faker uh that would be a good name for this i gotta see what the faker attributes gonna have are because i don't know would be good for a product name base lorem ipsum text real text company internet miscellaneous maybe no that's not good user agent internet domain name now address form based hmm now what let's do just do warm lorem ipsum text that doesn't sound that fun products related to internet domain well this company name could work if i also tack on [Music] some type of like prefix like a type of clothing sweater pants shirt so i think i'm going to do that it's called company so if i go back to phpstorm company and then we'll add in let's do product prefixes this is an array sweater pants shirt hats glasses um socks that works so then we're also going to create or we're going to get a random key from that array and paste that into the that as well um and let's find out how to get a random key because i don't remember how to do that i know there is a helper that larval has it we can get that i'm sure there's one array random perfect so we can say use illuminate support besides it's not facades what is it it's just support r and then our random contact prefixes so there we have the name now the slug needs to match the name though so we need to generate that name beforehand and store it so now we have this is the name and slug we need to use the str helper sdr slug name as you use windows can you quickly let me know how you work on there i assume doc but with wsl2 wsl2 or yes so uh right now i'm using wsl2 uh with docker uh you were not in here earlier i'm assuming but it was awful uh usually it works fine it works great even um and i love having a unix terminal on windows it's the reason that i'm trying to work more with windows development um that in the computer that i'm using for this is faster than my laptop that i usually use for development but it's got some quirks still especially with docker there's times where it won't run things for the strangest reason formatting issue the fact that you open up your terminal before docker was running is another big issue um it just it has some quarks and you really need to be worked out uh they're pushing out nightly releases though a lot of updates for a lot of a lot of fixes a lot of um features they recently just pushed out a feature which lets you mount external hard drives through wsl which was awesome because i actually needed that the other day for something that i was doing and they just released the feature for it but it's it's good um i i i'm liking it windows is taking a lot of steps forward in the development community and i really appreciate that for them uh have you worked much with pod man for setting up your containers no i haven't heard of that before is your project on a unix file system or windows partition it's on a windows partition right now and then we'll be recording yes this will be recorded on both twitch and this youtube channel and a video for this whole process will be out later this week i always use get bash on windows if you like the mac desktop look dual boot with elementary os has a super simple install that says dual boot really well yes i love elementary os i was using that um not too long ago but the biggest thing for me and it's going to sound awful but um i don't like i i'm so used to the mac keyboard layout that the control and alt keys being swapped for things like control c uh control v control tab i like the the mac layout of it to be able to hit alt tab to switch between windows and alt c and alt v to copy and paste that i could not find a reliable way to do that in elementary os was to swap the control on all keys it was always either like a hacky fix that only worked some of the time or it was just for like the uh copy and paste command so until i can figure that out i i'm on windows and i i like i'm liking windows with wsl too um a lot recently but yeah nothing nothing will be the native the native uh linux system especially we're working with docker it's insanely fast okay so we got the name and slug uh description which will probably just be i think faker does like long texts text real text let's do real text max characters this is going to be fairly long so let's do 256. that seems like a good length for a description actually let's do 320. and description price it's a number that's what they have number between so let's do a number between these are high-end items these are high-end items we're going to do a number between uh 10 000 which when we divide by 100 will be a hundred dollars and a thousand dollars okay uh and that's it so the time stamps and everything will be generated by default for us i believe so if i go to the user factory that's generated yeah it's generated by default for us so that is our product factory and i can use that to generate a new product with those fake attributes i'm using phpstorm but one problem i'm facing extension.belay.php doesn't appear when i press right click do you mean when you're trying to open it from like a windows so if i was if i was um yeah i probably should use decimal but uh we've already run the migration to store it as an integer so it'll probably bite me in the butt later we'll find out so are you talking about um bernard if you're in like uh you're saying that the blade php extension doesn't work are you talking about how uh if you're on like the file system you don't see that php storm is able to open this up or are you saying that like in phpstorm itself there's a problem with the blade extensions if you let me know more about it i can try to help you out you don't use come after the last arrays element i mean i could but usually i don't oh i know that's bad we'll use it for you guys okay product factory is done order factory this is going to be a little harder so we need a user id we need a transaction id we need a total so that's not that hard to do at least for two of those so user id will have to generate that on the fly excuse me and then transaction id i'm just going to use a random string for that using the scr helper library we can just do sdr random and we'll do a 16 length string and what was the last thing total and like before we'll just do this faker number between and we'll do a number between 500 and 2000 once the decimal gets added into it so the biggest problem is going to be this user id there's two ways of doing it um actually i think i don't know if there's two ways of doing it but the way that i always do this is that if i know beforehand how many users i'm going to be generating in the database um because our users right now are using um primary keys that are ascending in value from one i can just create uh or just pull a random number out between one and however many users i'm creating and use that as a user id is it great no but it works every time that i've used it as long as we are for sure generating the amount of users that we're expecting to which for this i'm i'm going to i'm going to hard set the amount of each model that i'm going to be generating in the cedar classes so this shouldn't be a problem so all we're going to do is run this faker number between and i'm generating 10 users so 1 and 10. and there's our order factory php storm itself when you try to create a file well i don't mean to go off topic real quick but yeah so if you're in your views folder here if you want to right click and new file the easiest thing to do so you might not see it here um which i don't see either i don't sorry i thought i had a sneeze there it came ah allergies okay um but if you hit new file here and you name it something like all you have to do is just name it like uh default.blade.php and it'll notice it that it is a it is a blade file and it'll be available as the blade template here but yeah by default i don't think under the new section there is a blade file here which i kind of don't like thank you okay um so back to where we were we got the order factory done we got the product factory done let it let us work on the category factory so if we look at the categories table it's just expecting a name and a slug so like what we did with the product factory we're going to have a name and a slug and that slug is going to be the slug version of the name which we will generate on beforehand and so our name is going to be this faker we just need like nothing too crazy real text of like 10 characters that should give us like i don't know a couple of uh couple words or one big word so that should be good i don't know why i put a comment there that should be a uh semicolon don't forget the quantity did i feel the quantity on something order factory oh no that's on the the relationship table so we will run that during the seed process um i hope we'll figure it out along the way combine random text with barcode in random order okay uh category factory is done product factories and order factories and user factories done so now we can actually start using these factories to generate data for us and the way we do that is with something called a cder so we can go down here to our database theater here this is by default coming out of laravel and what it does is is that you call under this run method the factories that you want to use and using this create method here it saves them in our database which uh we could do it this way we could do it on the fly and create all the fact we just paste in the factories here and use great methods but i like using a separate cedar class for each model that we're generating it just keeps things kind of separated and i believe it'll make using relationships easier as well so let's go to riding cedars excuse me and so what it's going to do is it's going to have a second run function here which is what we're going to use with our factory so user factory times and then this allows us to tack on um relationships to it and paste in the amount of relationships that we want to do it with it and then we can just use the we can just use a the class itself and the call method for the main database seeder and then just run dbcd and it'll actually run the seeder separating announcing classes also allows us to run just one seat or two so if we don't want to generate the entire database tables at once we just want to generate more users or more products we can just pass in this class method here to generate just the class that we want so let's uh let's create some cedars open up my command line and use artisan do i need to pass in anything for that or is it just is it literally just yeah it's just the name okay so user seeder is done product seeder category seeder and order cedar and we might not use all of these actually because a lot of these have relationships with each other we can uh we can call factories from one seeder i'll populate both tables that we need through just that one class user product order category okay excuse me so now in this run function here we can run this call and we can call in all of our seeders that are separate so each time that we run just dbc'd we want to populate the database with all of our seeders including users here glass product seeder class quarter seeder and category but again like i said uh we might not use all of these i'm just adding them in here because this file is already open you should definitely try to move your project into the linux file system performance is not compatible with what you're using right now i 100 agree i definitely definitely agree with that are you able to point me to the right video for getting larval set up on docker uh there's a lot of those out there right now so if you want to use the system that i'm using if you go to my youtube channel um they the introduction video on my channel is the first video that i made about getting larval set up with docker and then there's a follow-up video to that as well which kind of uh builds on that video shows you more things to do with docker and the larval setup and it comes from a very novice place with both docker and larval you don't need to know a lot for either or to get started with it additionally there's also the software lara dock there's laravel's new sale package which gets you set up with a local environment and docker with one single line which you can look up on their actual installation docs now lots to get started with will this video to be will this be a video to on your channel yes lars um so everything that's in the stream right now the source code and the whole gist of this stream is going to be compiled down into a video that i'm working on this week and that will be up and ready by next weekend so i'll probably be releasing that around friday next week and it'll be condensed into about 30 to 40 minutes yeah lardoc is really good um it's just the complaint that i've heard i'm parroting it because i agree with it and i agree with a lot of people who have said this it's very complicated there's a lot there which is good because i mean every laravel application has a bunch of different software that people could be using for it you know each one has its own requirements but for just getting started with laravel and docker it's a lot to work through but it's powerful very powerful and i like it so okay proxy editor so in the run function we are going to need our seater class we could do app uh was it database nope models is it product factory is that how i run this using model factories yeah so it's just the product it's just a factory method on the model so you should be able to call it product factory and then however many times we want to run that factory so however many products you want to create in the database uh so the product i'm probably going to create like 20 at first that's good for us and then oops time times and then so at the end here if we just ran factory times what that does is it it runs that factory 20 times and creates 20 fake models for us but doesn't do anything with them and so in order to actually save it to the database we have to run create and so what that'll do is it'll take that generated data that it makes for each time that it ran that factory and put it into our product table in the database what are the pros of using docker never used it using normal laravel p if you're with valet right now um so for me i kind of skipped right from a local setup on mac os which ran a lamp stack um directly to docker i used i used valet a couple of times but it um it works good for local development but i wanted something that really mimicked what was on my production environment so my production servers you know they ran php7 i think when i started learning dockers like php 7.1.2 ubuntu um it ran nginx not apache so i wanted something that ran the same setup that's why docker really appealed to me but the biggest thing for me is that um if i need multiple different larval applications that each have different requirements um so i don't know about you guys but i work with a lot of legacy software i work with a lot of a lot of applications that should be updated but haven't been updated in sometimes years and so some of them won't run on newer versions of php or some of them require newer versions of php or other software and it's it's using docker and docker compose files is really easy because you can specify all the software that you need for your system and you can spin up and down in literally minutes and so if you have if you're working half the day on one legacy application that needs php 5.6 and an older version of my sequel and you know even an older version of npm is that you can get that environment set up on your on your local environment that matches whatever production environment that it's currently on and then all of a sudden you switch gears to something that's a brand new application and you want to run php 8 or 7.24 and you know new mysql8 then you can just spin that one down go into your project stock or compose file and spin the new one up and have an environment without having to you know use something like a php switcher for mac os or wamp for windows it's just it's a way easier what i'm looking for it's a way easier set up i it's just it's just it's nice and clean and then if you start using docker when you actually deploy your excuse me deploy your projects it's makes it even easier i don't like working with a lot of sys admin stuff i've never been great at it so docker kind of took a lot of that configuration and headache out into a more simplistic environment yes this broadcast will be saved on both twitch and youtube so you'll be able to check it out on both can you explain why use nginx not apache honestly the only reason that i use internet server apache is i like the config better i i like the config files for nginx and the pathways and everything is just cleaner and easier to get set up than apache has ever been for me um but that's that's literally the only reason that i use it as opposed to apache i like it that's that's basically it okay so we have our product created 20 times um now we need to create relationships as well uh as just these models because we we could create products and then categories and then orders and then users but for our categories and products we have no way of telling lauravel that these two are related to each other and the reason for that is that that intermediary table because it's a minimum relationship it's not populating like you would if you're generating um products and categories through the back end of of laravel and so what we need to do for that is we need to call a method that is um generated partly with larval and then partly with your your model name and so that is if we go to our cedars class here it's this has posts right here so what that's going to do is if we call um has categories on um on the product sorry i'm getting kind of hungry and it's slowing down a little bit if we call house categories on the product seed and then we pass an integer of how many categories you want to create it'll create those categories for us but the downside is that it's not a one-to-many relationship so we i don't know if we want to use that or not um but this would this is what we would use for the uh users seeder to get them orders so each user has multiple orders but because each order only has one user we can call the user factory and say has orders and then pass in however many orders you want we want to on that can you explain why it didn't include the foreign key in migration um no i uh i probably should have i just kind of left it out and was trying to get through the migration as quick as possible but yeah okay um so i'm actually going to get through these cedars really quick um products either the order cedar the category cedar and the um user seeder and then i'm going to grab some food because i'm starving so after that i'll bring the stream down for a couple of hours or for like an hour or so and then um boot it back up and we can continue on with where we left off so for our product we have power factory running 20 times to create we're going to use our order factory here and we're going to run order factory and we need let's say so we're generating five users i think is what i said or what i had originally put on there it was in the factory that i did that uh order yeah 10. i'm doing 10 users so for the orders i'll generate 10 orders category um so we're generating 20 products i think them going into four categories should be nice i'm going to create those and then the user seeder users user user factoring and we're going to create 10 users as well so we have 10 users being saved in here we have 10 categories we have 10 orders we have 10 products and what we can do is just for simplicity's sake or not simplicity sake but just for demonstration i will run this migration and then i'll show you what's in the database so that you can see that we've populated some of the fields but not all of them and then when i come back i will clear out the database and we can run the migration again so compose run our artisan dbc and it should run all the seeders that we just created unless it gives us an error back in which case it's kind of likely to happen database factors sc are not found oh i did not include the call to scr in category factory that's my bad there we go okay now see if we run this again the problem is that it's going to generate because users see their products leader in order cedar i went through fine it's going to generate more of those again so it's going to generate everything again but so now we will have 20 users 40 products 20 orders i think is what i did and then our four categories so now if we go to like artisan tinker i can show you everything we have in the database so we do if we run all products we can see that we have you know cool a cool name that we generated here for this using the business name we have the slug we have the description uh which is just real text uh the price this is 133 and 41 cents 342.72 cents up here and uh yeah this is our these are our products is it just mirrored four two nine seven the id i don't know why that numbers i could have i'm like i was a little freaked out i thought they created four thousand two hundred and ninety products but now the ids are are here it's um it's what it should be so that's uh that's good um but see the downside though is that we don't have relationships with these yet so if we do uh products app models product with categories get it's it's not showing anything for the categories see we don't um we don't have that relationship built in yet so that's what we'll tackle next product count yeah see if we do the product challenge we only have 40 which is good okay so i'll just answer some questions before i head out here because nginx is better than apache yes okay um how to use larval with view in the same and view in the same project um so view lends itself really well to being just injected in in html in just um plain template files so what i'm going to do is um is that in our views file if we just open up a view here and i'll i will you know erase this view and create a new one if we just add in like a div that just says like you know id of app and then in our javascript on our view application and we just run or in our larval application if we just run view and initialize it on that app then we have a view instance running inside of laravel on this app and that's what's what's going to end up that's what's going to end up happening is that we're just going to have a single view for this entire application because the bulk of it is going to be running through an api to get the data and everything and then view otherwise light keeps going up and then view is going to pull in uh and display in that single blade template and then render everything that we need to render it through view files do you use refresh view cli i am using something like resources js frontend root of ucli app uh i use view cli whenever i'm building view like as a separate application so if i was splitting this out completely and having just a um single view spa running and then a separate completely separate repo for like um the larval api then i would use vue cli but uh for this i'm just going to i'll show you that i just use npm to pull down view and kind of get a boilerplate app up and running through through just uh just typing out text or just typing out code if you if you view a full schema design of the shopping cart yeah i'll um i will download because i actually don't have it on this computer and i realized that before i started the stream i realized that after i started the stream i don't have any kind of uh sql inspection program or anything like that running on this windows machine so i will download that so we can see the actual database um be able to sync it with it a little bit more thanks for the useful videos what drives you to make such good content uh so i learned web development from people doing what i'm doing now i am a self-taught developer never went to school for it so i'm just trying to give back to the community that got me where i am today uh laura casts basically got me interested in larval jeffrey way and all of his content and that's what i'm trying to to be like you don't use a database tool on phpstorm i completely forgot there's a database tool in php storm it's because i have these bars off most of the time that i completely forgot that there is a viewer directly in this let me think of how to open it back up again because yeah that'd be a good idea if i could just do that data source why don't i see my sequel because it's a whale or dolphin name localhost sounds good host localhost port is 3306 users homestead password secret homestead oh wow look at that thanks robert for that suggestion so look at that i don't need to download anything additional perfect so we can see our orders and we can see that if we go into the category product yes we don't have any we don't have any uh ids attached to the category in the product so that's why that relationship isn't working even though we have both categories and products we don't have um that relationship same with the order product table we don't have that relationship added in so we will get started on that after the break and i will be back in a few minutes you
Info
Channel: Andrew Schmelyun
Views: 4,301
Rating: undefined out of 5
Keywords:
Id: Y-nKnWqDeX8
Channel Id: undefined
Length: 98min 16sec (5896 seconds)
Published: Sun Dec 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.