Laravel: Creating Artisan Commands

Laravel: Creating Artisan Commands

in this video we're going to be talking about artisan commands and how they might be useful so if I switch over to my terminal here I'm going to run PHP artisan and that's going to give us a list of all the default artisan commands now what if we want to add our own well we can do this with a PHP artisan command make and I'm just going to look at the help for this so here you'll see that we have one argument and that's the name of the command then we have some options down here which we don't have to use but I'm not going to go through all of these but do take a look in your own time so I'm going to say PHP artisan command make foo and if we look in our app commands directory you'll see that command here so there's a couple of things we need to do in here before we can start writing our command the first thing we need to do is set our command name so I'm going to set this to foo so when a user or when a developer types PHP artisan foo that's when our command will be executed the next thing that we need to configure is our description I'm just going to say lorem ipsum we're going to skip this for now instead we're going to go down here and we're going to comment this out we're going to be looking at this later so don't worry and I'm just going to instead return an empty array and the same for the options and there we go so before we start writing a command let's go to app start artisan and down here we're going to say add this and add and inside here we want to pass a foo object so you want to instantiate that like so so inside the terminal let's run PHP Addison scroll up and here we go see our command right here so we can run PHP Addison foo and of course nothing is going to happen because we didn't do anything inside of this fire method so this fire method will be called or invoked when our command is executed so what we can do is we could say this info foo has been fired run PHP add some foo and we get a little message here we can also do things like error so let's go to our terminal and run PHP artisan foo and there we go we get foo has been fired and it looks like an error message so back in here I'm going to show you one other thing I'm going to say if this confirm and will say are you sure and we'll say this info you are sure so let's run PHP artisan foo and we get this little confirmation little message and we can type yes and then we get you as sure so let's clear that this is all well and good we know how to create a command but well we haven't looked at arguments in options and I don't think that we can truly learn without looking at a real example so I'm going to go to my terminal I'm going to create a new command PHP accent command make I will call this a generate user inside of here we need to we need to set some config I'm going to say a user generate like so and that description will be generate a new user okay so in here we'll just say for now this info user generated and of course we're lying we haven't generated anything yet but let's not worry about that we'll add this command in our addison file so I just an add new generator user now if we run PHP artisan you'll see our command right here so let's run it PHP addison user generate and oh we didn't we didn't clear the arguments but nevermind we're going to add some arguments now so if we look at the get arguments method we are returning a multi-dimensional array containing our arguments and in this example we want to pass two arguments we want to pass user name and an email so I would Vela packet type HP Addison user generate and we would have user name here and we could also have email like so and then we may want to pass an option like password equals test or something like that so here we're going to have two arguments like we discussed we're going to have user name and this will be required so we're going to leave that as is and that our description will be this that uses a username and we need to escape this so we're going to duplicate this and rename this email and there we go and let's just get rid of this for now and again return an empty array let's run PHP ads in user generate and you'll see that we get this message here and we also get the correct usage of the syntax of the command so use the generate username email sorry PHP artisan PHP artisan user generates username email hit enter and we get our user generated message so up here what I want to do is I want to first before we do anything just output one of these options are these arguments sorry so to do that we call this argument and then we pass the argument name so let's say username so PHP ads and user generate or say Terry and I'll just pass in some random data and there we go get our username so let's clear that and now we're actually going to generate the user so we're going to say we could either use our eloquent model here or we could use the DB class we're going to use the eloquent model for now just to make things simple so we'll say user equals new user username equals this argument name and keep in mind that you may want to do some validation here you may want to check if the name or email has already been taken and if it has you just have a conditional statement and this era is named at reserve that something like that so user email equals this argument email and then we're going to say user safe and then we're going to say this info user has been generated and just to make this a little more personal I'm going to say a user user name so let's run this page P Addison user generated I will put in Terry and we'll just put in John at and we get an error because I accidentally named this wrong so this should be user name in fact just because this is called name I'm going to rename this to name just a picot edge just to be consistent okay so let's try this one more time hit enter and there we go user terry has been generated so let's look in our database hit refresh and there is our user now of course we want to add a password too but we probably want that to be optional so down here I'm going to grab this I'm going to uncomment this and I'm going to say password and down here we're going to set the example to the users password so let's have a look at these arguments really quickly the first one is as you probably already guessed the name of the option the second one is a shortcut which is basically as it sounds a shortcut to the option the next argument is the the option type so in this case it's going to be optional and we do want this to be optional next we have the description and finally we have a default value and in this case I'm going to set the default value to pass so up here I'm going to say user password equals and we want to hash this so hash make this option password so let's run page P adds to the user generate and here you see that we have this password option here so let's try it page P Addison user generate I will have the username as Billy and we'll have Billy at for the email and for the password we'll set that to and you know what let's just quickly get rid of this so we know that our option is working so password equals test and there we go user Billy has been generated we can add database and there we have our name email and password

8 thoughts on “Laravel: Creating Artisan Commands”

  1. Best tutorial.

    Read complete Laravel artisan commands list –

Leave a Reply

Your email address will not be published. Required fields are marked *