Laravel Advanced 2021 - Jobs, Queues & Events Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome to a brand new series in this series we are taking a look at events and jobs and queuing and basically taking your level application to the next level when i first started in layerville one of the things that attracted me to level was the ability to do this offline essentially not make your users wait for you to do a computationally expensive operation now this can take the form of sending an email for example we think of sending an email as a fairly quick thing but when it comes to page load times waiting five seconds for an email to be sent well that is a long time so that is when you queue off emails another thing that drew me to laravel was the event dispatcher and the ability to have these events these things that happen in your application that trigger a bunch of other logic this is something that when you're first starting out seems like such a difficult concept to comprehend but now that we're working on some advanced level topics i want to take the time to build a contest and what this is going to be is imagine that we were running a daily contest where somebody could come in type in their email address and then every day or every night at some point we pick a winner that winner gets an email and so on and so forth now there's going to be a lot of rules we're going to add to this contest eventually basically emailing out maybe an excel sheet or something like that that accumulates all of the winners for that week and all of this is going to be done offline so the front end of the application that we're going to be working on is going to be extremely simple it's just going to be a box where you can enter an email the rest we are going to handle in the back end and it's all going to be automated and once it's up and running we're not going to have to do anything to it whatsoever so i have a brand new laravel 8 application here i will cd into it i called it contest and one of the only things i really want to do is go ahead and bring in this new breeze package which used to be laravel ui it's gone by different names it used to be part of the core but all we need to do is just require level breeze now all you have to do once level breeze is installed is we just have to run the breeze install command we can find that under php artisan and if we look in here you'll see it right here breeze install so we'll go ahead and run that command and basically all that does is it does the typical scaffolding that you're used to and it does bring in tailwind so we'll go ahead and do our npm install and npn run dev now this series is not really intended for those that are just starting out with level we really want to tackle the medium to advanced developer that just wants to step it up and take this to the next level so we're going to skip a lot of the basic things that we typically cover such as what npm is what package dependencies all of that we're going to skip through all that that is all beginner stuff we're going to tackle the difficult stuff so i'll let this go ahead and finish and i'll be right back and here we are now you may see this from time to time i think it's always worth exploring you could just run this command and it will just upgrade it for you i won't do it in the video but i typically would go ahead and do that so with that being said i am running level valet so i should just be able to visit contest.test and move on from there so you pull open my browser and there it is so contest.test all it has is just my beginning page and we do have the breeze package so we have login and register which to be honest at this beginning stage i don't have any plans for it but i really did want the scaffolding if we check out what we have now we'll move over here open up contest under resources and views we do have all these nice components and dashboards but more importantly we have an app.js which brings in some important things as well as a package.json which has some scripts as well as brings in tailwind and axios and a lot of the things that we're going to want to use so this gets us started with this project i'll close that up and let's go ahead and get started and run our test suite now i am assuming that this is not going to work and sure enough we have an issue and it's a very simple one it simply means we don't have a test database we can go into our php unit configuration file and starting in version 8 you do have these two lines right here that are commented out i will go ahead and bring those back in and all it does is it gives you a db connection of sqlite and in memory this is going to be perfectly fine for our application i'll go ahead and reformat that so it's nice and clean and let's rerun our tests and sure enough everything is working now we do have a lot of tests in here and these are just brought in from breeze we have new tests now that takes care of making sure we can do password resets and email verifications a lot of other cool things for our purposes we do need to make a new one so php artisan make test and let's go ahead and make sure that we can actually add this email address that a person can enter we'll go ahead and say contest registration test and we'll go ahead and open that up so contest registration and here we are i can go ahead and get rid of this top line don't need that we'll add a new test and it will simply be an email can be entered into the contest there we go we will hit an endpoint which will be a post endpoint and that is going to be contest and we're going to hit that point with some data really i just need an email and for now just abc at abc.com all i need now when that happens i do need to make sure that i have a model and then i can make sure that i look in my database and i do in fact have that email registered in so we'll do is we'll say this assert count of one and if we look in the documentation for database count we need the table first and then the count so we'll go ahead and pass that in first i'm planning on calling my model contest entries so i will say contest entries and i assert a one all right let's go ahead and run this and of course we have an issue right away there is no such table as contest entries php artisan make model contest entry and i should have created that with a migration so i'll actually go ahead and remove that really quick that way i don't have to do it later so app models contest entry go ahead and remove that i'll run the command again but this time i will need a model let's go ahead and create a factory while we are at it so i'll go ahead and create all of that all at the same time and sure enough now i can run the test again and there we go so no search table as contest entries now the reason for that is that we are not refreshing the database so i'll use the refresh database trait and now new error fail is certain that the table contains the matching count of one we only found zero and of course we did because this doesn't even exist of course we know that is happening because we are taking care of exception handling so let's go ahead and run it without exception handling this time and our error should be very different and sure enough it says post this doesn't exist so we know that go ahead and open up our web routes let's add it right here actually it's gonna be a post route there we go contest and let's hit the contest entry controller and the method is gonna be store run that again contest entry controller does not exist php artisan make controller contest entry and then if we run it again now we should not have i'll tell you what it is there we go so this is going to take me a little while i've been writing laravel for a long time and this new change in level 8 is going to take a little bit of getting used to so let's go ahead and switch over to the new notation which is going to look something like contest entry controller class and then as a second argument in this array we'll pass through the method name in my case it's going to be store all right let's try that again and now we do get that store does not exist so just a little change but the nice thing now is that i can click through right here which is the nice part it's a nice trade-off that we have so store and here we go now we end up back here all right so we are going to do some validation so we'll say data and we'll go ahead and grab request which we don't have yet let's go ahead and request it right here we'll say request we'll pass that through as request so request validate and the rules are going to be very simple for now just email and we'll leave that blank for now we'll go ahead and put everything under test so we'll leave that blank and then we're just simply going to store that in all right so we'll say contest entry and we'll create with this data and there we go let's go ahead and give that a go let's see what we get now so mass assignment not a huge fan of mass assignment so i will go ahead and turn that off altogether so we'll say guarded equal to none i'll go ahead and open up the whole model and now we have contest entries there is no column like email okay so create contest entry contract entries table and to this table we will add a string for email and there we go we have one passing test it doesn't do much yet obviously i want to put a couple of more things under test and the first one being that it is required so let's go back to my test and let's write a new one here that says email is required we'll go ahead and get rid of this exception handling we don't need that anymore we'll go ahead and copy all of this and we'll actually pass through an empty string right there then i expect zero at this point let's see what we have oops i need to grab this one email's required and it's passing that is not right obviously we expect this to be a zero so why is that passing well let's go ahead and find out we'll save this without exception handling and there we go okay so we have a constraint violation basically saying we have an issue email is not nullable so we need to go ahead and fix this and of course this has to do with the fact that we are passing it through because we don't have any validation rules so let's go ahead and make this required and now we get email is required instead so if we turn off validation exception now we get a green test again we can run the whole test and we're good so at this point and stage i feel good enough that we can actually go ahead and create the ui for somebody to enter their email into our contest this is going to take a very simple shape as i said already i don't want to worry too much about the front end so we are going to uber uber uber simplified this whole entire thing so i'll go ahead and grab all of this right here and it's all gone i believe this is it a couple more there we go and we're going to say welcome to our contest all right let's see what that looks like in the browser hit refresh welcome to our contest perfect because we are using tailwind i'll go ahead and style this really nicely very quickly well a couple classes here one of them being a large font size and font bold so we'll say font maybe semi bold and we'll also say font maybe 2xl i don't know what is actually available to me what we can do is maybe in a different tab i'll go ahead and run a watcher so npm watch whoops npm run watch okay let's let this run in the background and with this compiled we should have text to excel and we should also probably make it lighter so we'll say text blue 100 maybe 200 let's try that it still doesn't work and actually looking through it i realized that we are not even pulling that in now if we look through resources views under layouts we should have this app that blade and if we look in here we need to copy the styles and the scripts i'll go ahead and bring that over right here we'll paste that in right there and now there we go we are back in business we just lost the color of our background so to do that we could probably change it right on the body here we could say bg blue maybe 800 and that's not taking effect because i probably have vg gray right here 100. i'll get rid of that let's try that again there we go maybe even a little darker let's go to 900 and there we go all right so welcome to our contest this will have a simple input field which we'll style later on but all i know is that the name of this needs to be email and then we'll have a button and the button is simply going to say enter now again styling wise we can really do a lot with this but we do need a form so let me wrap that in a form and this is going to slash contest and here is my form hit refresh there it is doesn't look very pretty so let's go ahead and flex this as a container let's go ahead and do a flex column there we go so that's looking somewhat better it's not the greatest but it'll get the job done so when i enter something here abc sure enough it goes in there but it says hey this is a get method it's not supported by this route we know that let's go ahead and change our method to post and while we're at it let's go ahead and do csrf we know we're going to need that hit refresh type something in enter now and there we go so it says that contest unknown database we already resolved the database issue we just have not really done it outside of testing we resolved it for testing but not for this part so i can do basically more or less the same thing i can go into my environment file and i can specify this to be an sqlite database perfectly fine for our example and so all i would need at this point is just to create that database so inside databases i'll create database dot sqlite and i'll migrate my database all right refresh and there we go it did work in fact i can actually go into tinker you can't really see it let me go ahead and switch it over let me go so inside of tinker i can say contest entry all and we do have that in there now one thing that did occur to me is we don't have this to be an email validation we need to put that under test so we'll do that and then we'll call it a day we'll say email needs to be an email pretty self-explanatory but we need to make sure that validation is in place we'll type in some gibberish and we still expect zero all right let me exit out of here run our test and there we go and of course sure enough we have we expected zero but we got one and that's simply due to the fact that we have the wrong validation so i'll go ahead and make it required and make it an email now this is some very light testing we may go back in the future and do a little bit harder testing on the particulars of what gets put in and all of that but to be honest that is really outside of the scope of this i'm just trying to get this project ready for us to actually start tackling some events and all that so not overly concerned with this part of it i think you know that you could do a little bit more robust testing and sure enough we are passing we'll pass through the whole thing and there we are so that's gonna wrap it up for episode one just a quick setup on this project as well as explaining what we're going to be working on but in the next episode we're going to continue to dive through this project and dissect what we're here to talk about which is the events the acuity events and everything else that we have in store alright so i'll see you in episode 2.
Info
Channel: Coder's Tape
Views: 37,140
Rating: undefined out of 5
Keywords: Laravel, Laravel 8, Laravel 2021, queues, jobs, events, Laravel advanced, laravel 7, mailable, tailwindcss, laravel email, markdown mailables, laravel mailable, laravel mail, laravel for beginners, learn to code, laravel send email, php, laravel tutorial, laravel project, tutorial, php framework, explanation, meaning, definition of mailable
Id: DFXNYI0iy1g
Channel Id: undefined
Length: 17min 22sec (1042 seconds)
Published: Thu Feb 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.