In this video, learn how to write a new Laravel Artisan Console command and how to use your Laravel application from the command line.
- [Instructor] The artisan console that we've been interacting with in our terminal is just another interface to our Laravel application. We'll see that over the course of this set of videos. Start at your home set box and navigate in your command line or terminal application to your home set box, an sshm with vagrant ssh. We'll then change to the code directory and from here, you should be able to run the command php artisan. There's a variety of actions we can take here. We've already seen how we can use artisan make to better generate some basic files for our application. Artisan also has a superpower that we haven't seen yet. This superpower is called tinker. Tinker gives us the ability to execute a repel in Laravel. A repel is a read, evaluate, print loop. This is where our Laravel application via the console can read some code, evaluate or execute that code, print some output and loop back around to reading again. Lets see this in action now. Run the command php artisan tinker and we're here in Laravels built-in repel interface. Lets try entering a basic model find. For instance, lets do App\Room::first. And you'll see we find the first room. You'll see we'll get the room number, the room type id, it's created at and updated at values all available. Lets try something different. Lets get the rooms first id. So if we erase the semicolon and then add an arrow id, we'll get one for the first rooms that we can find id value. We can also execute basic php here as well. It doesn't have to be Laravel focused code. Lets do some basic math like 10 modulus operation three. This modulus operation using the percent symbol, if you haven't heard of it before, returns the remainder from our division operation so 10 divided by three is three with a remainder of one in this case. Essentially, any line of valid php or Laravel code can be executed in the repel. We can even stack operations together. Lets do variable x is going to be equal to 10 parentheses three or 10 modulus three, will then have x increment by one doing x++ and then we'll echo x and we'll get two. Pretty nifty. So that's tinker. Lets get out of this using the command exit. Laravel provides tinker, make and other console commands for us. However, we can also provide our own commands in addition to the standard Laravel ones. This can make it much easier to call certain actions on a repeating schedule. For instance, maybe you might want to send out emails the day before all the reservations in your system to notify the guests that they have a reservation in the system and include details. For instance, how to get to the hotel. You could use a Cron job which is basically a way of telling your servers to do a job on a repeating schedule to execute this Laravel console command on a regular schedule. I'm not going to cover the Cron job portion of this but we will cover the Laravel console portion of this. We're going to make a command much like how we've created every other class up to this point with running the command php artisan make:command email reservations command. This will create our console command and we can edit it now with our text editor. We'll go there and open up the created file at app\console\commands;emailreservationcommand.php. Like all standard Laravel classes, we get some out of the box basics for us. The name space is predefined. The base class and some other stuff already provided. On line 14 is the signature. This line lets us tell our Laravel application what CLI command should execute this command. Think of it like an alias. In essence, we say when we type php artisan signature, please execute this command. Notice our command includes two parts. This gives us the ability to organize our commands. For instance, we could create this under hotel or reservations or email or maybe just the name of the application we're building and then specify the command further. In this case, I'll do this with, I'll name the command reservations and replace name with notify on line 14. On line 21, we'll add a description with notify reservation holders. Lets see what that turned up. Go back to our CLI application and run the command php artisan list. You'll see here our reservations notify command is available and if we try running the command with php artisan reservations:notify, nothing happened because our command shouldn't be doing anything at this point. But now we have a console command available for us to iterate upon.
- Saving and viewing files
- Displaying validation errors
- Console commands, outputs, arguments, and inputs
- Building a Laravel route that has multiple parameters
- Working with the Laravel authentication system
- Preparing for deployment