Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e34 – Artisan Commands – Part 1

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

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!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue