How To Use Queues In Laravel | Introduction To Queues | How To Use Queues

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up developers it's dory here and i hope you're having a great day because we're going to talk about cues in larval quick pause do you want to support the channel and want me to continue on pushing content well you can support the channel through patreon right now you also get benefits just as a private discord group where everyone is helping each other out if you are interested to join the link will be in the description down below when you start working on complex and bigger web-based applications queues is a topic that is pretty important on this channel and something that you might have done before as well we worked with one request at a time but what if you get a hundred or even thousands requests at the same time with implementing queues you're going to tell your application how to perform a particular behavior the best example might be mail usage if you have your own e-commerce site and customers purchase products do you want them to wait for the confirmation email or do you want to make sure that they get it immediately or even a delay let's say that you want to add a delay of 2 minutes before they get their confirmation what you're eventually going to do is to trigger a send email cue job so a user gets a confirmation i've created a new laravel project from scratch and there's also tailwind added to it but we're not going to use it for this video but i don't want to perform the same steps every single time to create a project so you can clone my github repository i will link it in the description down below so let's go over the steps one more time you could either click on code you can either click on code right here copy the github repository or you could download it as a zip but we're going to clone it so let's click on the copy button before we navigate to the terminal let's close it off and scroll down because i've added a usage part right here how you could install or set up this project today's video is brought to you by cloudways a managed hosting platform for your applications cloudways has enabled businesses to scale and reach new milestones instead of worrying about server hassles with reliable 24 7 support cloudways offers complete peace of mind from surfer level nightmares furthermore its superior tools and works enhance your productivity so you can focus on creating incredible experiences with your applications it's a fast managed solution for agencies smbs and e-commerce businesses what i personally like about clyde ways is their slogan moving dreams forward if we then navigate to the location where you have stored your laravel projects through the cli so let's say that i have item i have a command called cdw for my workspace and in here we need to perform git clone and pass the url that we just copied if we hit enter our project will be cloned right now and this did not take long because the project does not have composer installed what i want to do is change the folder name because if we perform an ls right here you can see that the project name is larval8-tailwind css2 this can be done with the mv command this will either move a folder or file or you can rename it so what we're going to do is to say mv larval 8-tailwind css2 space and we're going to rename the folder to larval 8 dash queues let's hit enter let's cd into our larval 8-qs folder let's install composer right now by running composer install i didn't push it on purpose to get since it's a pretty heavy file this might take a second all right it's already done now the next step is to create the emv file based of the emv.example file so what we're basically going to do is to duplicate it with a different name by performing cp so copy the emv.example space to a file called.emv hit enter alright the last step is to generate the app underscore key environment variable indeed.emv file so let's say php artisan key colon generate and our project is set up and i think it just saved us a lot of time so let's run php artisan serve if we open visual studio code i've already got it open but i need to open my project so let's say file open a new folder it's on my desktop workspace and we need to open larval8 queues now just like many other larval features there's a configuration file for cues stored inside the config folder so let's open it at the bottom right here let me make this a little bit smaller alright what this file basically allows you to do is to set up multiple drivers and define which one will be your default queue if we scroll down you can see that we got sync we got a driver called database a driver called beanstalked we got a driver called sqs and we got a driver called redis in this video we're going to focus on the database 1 which will create a database table for you but more on that later on if we think about our e-commerce example once again every single customer that purchases a product will be quote-unquote put in a line which in programming terms means a job so a job will be created whenever a customer purchases a product if we scroll to the top of our page you'll see that our default queue driver is being grabbed from the emv file where a variable is stored with the name q underscore connection so let's open our emv file to see what the value is of a variable q connection so let's see if we press command f and cq underscore connection you'll see that the default value is sync again like i said we are going to use the database for this so let's change the value up to database since we're already in the emv file let's also set up our database to save our jobs so right here i'm using my sql my host is alright my port is alright my database name will be larval underscore db we haven't created our database yet but we will do it in a bit my database name is root and my password is dari1234 now let's save it since our credentials are correct we could also create our database so let's navigate to the cli again open a new tab and let me zoom in inside the cli let's perform the mysql command to get access to our databases we need to create a new database of course so let's do that by saying create database called larval underscore db now that our database has been set up we could use artisan to create a custom queue so we first need to open a new tab and navigate back to our workspace where we have our cd larval 8 dash cues folder alright then inside the root of our project we're going to perform a php artisan make command colon called the job and the name of the file will be customer job i also want to add one more command which is the php artisan make colon mail command and we're going to name the file send mailable now these commands will create two new folders inside the app folder for us so let's navigate there if we scroll up open the app folder you'll see a jobs holder and a mail folder the jobs folder will have a file called customer job and the mail folder will have the sent mailable file now let's start off with the customer jobs file right here you can see that the customer class implements should queue if we click on the control button and go to the definition of should queue you will see that it's not a class but an interface that has nothing defined inside of it so it's actually not using anything from the file if we close it off you can see that our customer class has four trays that are being imported we cut the dispatchable which will create methods to dispatch itself we got interaction with q which allows jobs to control its relationship with the queue we got queueable which allows you to define how larval should push this job to the queue and the last one is serializes models which gives the job the ability to serialize and deserialize eloquent models if we scroll down you'll see that this class is also using a constructor which is needed whenever you're going to do something inside the handle method since it accepts the data that is needed now speaking of the handle method we're going to send a new mailable right here so let's remove the comment and let's call the mail method called colon2 we're going to made it well let's say to myself so info at darindazar.com we're going to chain a new method to it called send and what we're going to send is a new sent mailable which is a method as well and i'm seeing that my mail method hasn't been pulled in so let's type it again and hit enter all right as you can see both use statements has been pulled in so the send mailable and the mail vacate we are ready to set up our queueable table right now so if we navigate back to the cli and perform a php artisan queue colon table hit enter you'll see that the migration has been created for us but it won't migrate the file itself so what we need to do is to obviously run php artisan migrate if we then open our mysql tab use larval underscore db and then we're going to show all tables right now you can see two new tables that you probably haven't seen before the first one is failed underscore jobs and the second one is called jobs so let's desk jobs to see what's in here it has an integer which is an id a queue payload attempts so the amount of attempts a user has performed reserve that available at and created that right now we have created our basic queue but it's not working yet we do need to controller because we somehow need to trigger the job we just created so let's navigate back to our other tab again perform a php artisan make me a controller called email controller and we then navigate to the cli now let's open the controller so http controllers and the email controller what we're basically going to do right here is to create a method that will dispatch a job so let's create a new method called public function send email there are actually different ways on how you can dispatch a particular job i prefer to use the dispatch method on the job itself so let's create the dispatch method first and inside the dispatch method we need to pass in the job that we want to dispatch and in our case it's the new customer job then at the bottom let's just add a basic dd of email has been delivered so we obviously need to have an endpoint since that's going to be hit so let's open the routes folder the web.php file that's actually not to get rid of the route column column get method to the welcome page but let's create a new route column calling get the first param will be the endpoint of forward slash send mail comma brackets we're going to pull in the email controller column column class comma space single quotes because we need to call the method which is sent email now before we test this out we need to make sure that we have mailtrap setup to test it out so what i want you to do is to just pause the video add it inside the emv file and i will be back in a second when i have done it as well all right i've set up milter app and if we navigate to the browser right now if we open a new tab and navigate to our local host forward slash send email our dispatch method has been triggered and as you can see our dd of email has been delivered has been printed out and we then navigate to the cli and go to our mysql tab now let's select everything from the jobs table you'll see a bunch of random text right here which is actually not random you can see that the job has been added inside their jobs table which is primarily the case of what we're doing right now if we then navigate to mailtrap you won't be able to see an email but there is a new job inside the jobs table so this means that the process of the queue hasn't been started yet and of course there's always an artisan command to fix this so let's navigate to the cli and here let's perform a php artisan q colon work before we perform it let's take a second to just go over what we're actually going to do what this command will do is start listening to your cues inside the jobs table whenever it sees a job inside the database table it will pull down the first job handle it delete it and move on to the second one we need to change one more thing up right here if we navigate back to visual studio code and open our send mailable you'll see that it's returning a view to view.name this needs to be changed to welcome because the view.name does not exist so let's navigate back to item let's run the php artist in queue column work command you see that our customer job is being processed and there's also a new line which says that the customer job is processed if we now go back to our mysql tab and select everything from the jobs table again you'll see that our jobs table is empty right now and it has been processed another pretty cool thing is that you can customize the amount of time your q worker should wait before processing a job right now you just saw inside the other tab that is being done immediately but you can chain a method to it which will accept an integer which will be the number of minutes a job needs to be delayed and i do need to mention that this can be done in seconds as well if we navigate back to let's say visual studio code to our email controller right after our dispatch method we're going to change the delay method and inside the delay method we're going to pass in the now method we're going to change the add minutes method to list hit enter and in here we need to pass in a value of the minutes so let's say 1 save it if we then navigate back to the browser remove our endpoint hit enter and let's add our endpoint one more time our email has been delivered as well if we then navigate to the terminal you'll see that our queue is still active but it does take a minute so meanwhile let's go back to mailtrap you'll see that three minutes ago we did indeed receive the email which i completely forgot to show you which was from the previous queue now if we wait one minute so you just saw the time it was 0.55 and the email has been sent three minutes ago so it should be shown approximately around the four minutes time mark and right now you'll see that we received a new email if we open it it's the same exact one it's the same exact one but with a one minute delay added to it this was it for this video if you do like my content and you want to see more leave this video a thumbs up and if you're new to this channel please hit that subscribe button you
Info
Channel: Code With Dary
Views: 3,182
Rating: undefined out of 5
Keywords: laravel queue, how to queue email, queue email, why use laravel queue, why use queue, how to use queue, queue for laravel, queue jobs, laravel jobs queue, queue and events, laravel 5.8 queues, laravel email, laravel 8, laravel queue redis, laravel queue jobs, laravel advanced
Id: y2NiXD7nSUQ
Channel Id: undefined
Length: 15min 56sec (956 seconds)
Published: Mon Jul 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.