Laravel 5.8 – From The Ground Up
Lección 34 de 52 · Laravel 5.8 Tutorial From Scratch – e34 – Artisan Commands – Part 1 · Educación
Laravel 5.8 Tutorial From Scratch – e34 – Artisan Commands – Part 1
welcome back we are back on our project today and we're ready to tackle something new and that's something new is consul commence if we jump over to
the terminal we ran PHP artisan many times and we've ran all of these different commands but what we're going to be working on today is adding our very own command commands
have a ton of different uses and you can do just about anything with a command the command that we are going to be working on today is a command to add companies to our database if you
remember from our project let me jump over to our project right now even though we have a customers list we don't have a way of adding companies now of course we could add a whole new form to
add companies but you already know how to do that we talked extensively about restful controllers and how to get this done so I want to show you how to do it in a
different way so my idea is a PHP artisan command that we can run and add a company so let's see how we can tackle that first and foremost we're actually going to start
with a command and that is the make command obviously as we've learned before laravel has a great way of scaffolding things for us and getting us right to what we need to do so let's
make a command as always let's run PHP artisan help make command just to see what the command is about and it looks like a name is the only required argument so let's run that again and
let's give it a name let's just say add company command let's add that whoops looks like I still have to help on let me remove help and there we go console command created successfully all
right back in phpstorm in our app directory inside the consul's we now have this new commands directory and inside there we see the add company command awesome all right so of course
laravel has scaffolded everything for us so let's start with this signature this signature is what we're gonna run right after PHP artisan typically you will prefix all of
commands with something related to your project obviously our project is really just a dummy project it doesn't really have a full domain but we could say that this is about companies and customers
so maybe we can prefix this with contact and of course we're gonna say company all right let's run PHP artisan right now and if we scroll up we actually see our command right here
contact company how cool is that so laravel added that command for us automatically it is automatically looking for commands and adding anything that it finds inside that commands
directory so you see that contact becomes a grouping so as you see now make all of the make groupings are underneath make and you see that they all start with make that's the way it
works so now that we have that we can write a description let's say adds a new company easy enough so if we come back and run PHP artisan again scroll up adds a new company so we see our description
of our command in here what about PHP artisan help contact company yep we do have that and of course we don't really have any required parameters just yet we'll tackle that in
just a second let's keep looking through this file so we have a constructor if you need to add anything in the constructor that's where you would add it and we have our handle method and
this is really the one that we are concerned about I will actually remove our constructs because we don't need that at all and we'll just handle everything inside our handle method so
how can we add a company well of course we have a model so let's just say company equals company and that is app company it is being imported up here at the top use app company otherwise you
get a company class not found and then we can run creates and to create we can pass some arguments now let's take a look at the migration just to refresh our memory on what we have here so
create companies table all we need is a name and a phone number that's it that's all that's required so let's say name for now we'll just say test company and we need a phone number
so let's say 1 2 3 1 2 3 1 2 3 4 ok great so now that has created a company so how do we output some text back into the console well let me show you this I'm gonna actually comment that line out
because I just want to show you this on its own if you run this there's a couple of different methods that you can call one of them is info and all you need to do is pass a string info string here for
example let's run our command PHP artisan contact company and we see info string here okay let's duplicate this and I can use warn for example that's another one so let's
just say this is a warning I will output both so you can see the difference so there we go so this is an info string this is a warning and then we do have error and
error of course will be in red let's check it out so there we are so we have an info string a warning string and then an error so you can use any of these at some point so now that
we have that in mind what I'm thinking is maybe we could say added and then I want to output the company name so of course I can fetch that with company name because I am saving this create two
company and the create method returns the new model before I run this I want to check one more thing and that is the following I want to make sure that my company is gonna accept the create
method if you remember from previous episodes you have to make sure that guarded is set to an empty array or that you explicitly list out the fillable fields so in our case guarded is set to
an empty array so we are good to go before I do I do want to show you what we currently have in our company we only have ABC company and de F company all right let's
run that command now and see what happened PHP artisan contact company added test company obviously our message and if we go back to the forum hit refresh
we now have test company very cool so we were able to add a company through the command now of course we are hard coding these values and that is probably not what you would normally want so I want
to replace these with actual text that we can pass in so how can we do that what we can actually do is very similar to route files we can use the curly bracket notation and put in name ok and
then instead of hard-coding a string we can say this arguments and we just need to pass it the key so we'll say this argument name we will take care of the phone number in just a second I just
want to show you what we changed let's run PHP artisan help contact company and now we see that as an argument we require a name pretty cool all right let's do that one more time and add a
new company PHP artisan contact company and let's give the company a name we'll say that the company name is gonna be a new a new company alright a new company was at it we can obviously tell it
already worked because we could see that we added a new company so we received the string from our commit and if we go back to Chrome hit refresh we have a new company great now let's do the same
thing for the phone number but I want to tackle it a little bit different just to show you something else for example we can pass in another curly bracket here and say phone but you could
put a question mark right afterwards which actually makes it optional ok let's check this out back to the terminal PHP artisan help contact company so now we see that as an
argument we have phone but we see that this is optional we don't quite need it ok so let's do something here let's say this argument phone so if you provided a phone let's use that otherwise let's
just say and a fair enough let's try that command now PHP artisan contact company will just say bananas it's gonna be a company name and we won't pass in a phone number
let's run that now and now let's run another one but this time I do want to run a phone number in it so we'll say 1 2 3 1 1 1 2 2 3 3 pass that in alright so to show you that that worked let me
pull open table plus and check out our company's table so we see that we now have this bananas that says n/a because we obviously didn't pass anything into it and we have this one which has our
phone number that we did pass it so this first one corresponds to this PHP artisan command notice that there's only a name and no phone number and then this second record here was generated by this
command right here obviously noticing that we have bananas as a name but then we do have an argument which is the phone number I want to show you one more thing about that so back in phpstorm
you can actually give it a default value right there so we can say instead of question mark we can hit equal sign and then we'll say n – a and that will actually do exactly the same thing so
let me run that command one more time that was successful let's jump to table plus hit refresh and sure enough we still get an A so we can actually remove this line right here and just leave this
argument phone and again let's try that one more time refresh and sure enough it's still working so we do have that default baked into our command so that is the basics
of writing a console command in level so in the next episode I want to take it to the next level but Before we jump into that go ahead and play around with making your own commands and seeing what
other features you can add to our project using a command like I said a command can do just about anything so when you're ready let's jump into the next lesson and continue to work in our
command adding more advanced features to it I'll see you then
Let’s start talking about how to create our very own artisan command. In part 1, we will make a command to add companies to our project.
For the best experience, follow along in our interactive school at https://www.coderstape.com
DigitalOcean Referral
https://m.do.co/c/7dce5364ef4d
Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch
Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)
About This Course
Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!