Laravel 5.8 – From The Ground Up
Lesson 35 of 52 · Laravel 5.8 Tutorial From Scratch – e35 – Artisan Commands – Part 2 · Education
Laravel 5.8 Tutorial From Scratch – e35 – Artisan Commands – Part 2
welcome to part two of artisan commands as a quick recap in part one we made a command to create new companies in our database now we were passing the name
and the phone number as an optional parameter through the signature but I want to convert this into an interactive command meaning it will ask us the questions we answer them and then we'll
get a confirmation at the end it's a little bit difficult to explain so let's just jump right into it and I think you'll get it right away so let's actually erase those two
variables and let's do the following I will comment this out for now and right above it I will say name equals this ask and then what are we asking what is the company name I will actually comment
this out and then I just want to echo out name so this info name just to see what kind of results we get let's run our command PHP artisan contact company so what is the company name we'll say a
cool company and sure enough we get that right back well that's pretty cool alright let's do another one let's say phone what is the company's phone number alright and let's go ahead and echo out
phone let's run the command again so what is the company name company what is the company's phone number one two three one two three one two three four and sure enough we get that back how cool is
that this style of command is very helpful for those that are unfamiliar with our commands signature because they actually don't need to know the order or anything else the command will prompt
the user for everything else so now let's do a final confirmation let's say if this confirm are you ready to insert and then I want to put the company name let me put a double quote there now
let's add name and then and let's just put a question mark okay so if that is true then we will go ahead and eventually I will still leave this commented out but eventually we will
actually create it otherwise we'll say this worn no new company was at it that way we provide some feedback up to our user fair enough all right let's give this a go try one
more time what is the company name we'll just type in whatever company phone number no new company was at it okay I must have made a mistake here ah it's not configured it's confirmed there we
go confirm all right let's try that again company name company phone number are you ready to insert SDF yes or no if I just hit Enter we know that no is the default command and if I typed in yes
then of course we would have actually created our accompany I'll type in yes and no new company was at it fair enough I didn't actually return anything from here so let's go ahead and
do the following let's insert that record instead of arguments we know we actually have variables by the name of name and by the name of phone so we'll insert that and then let's return this
info add it and then we'll put company name so our command now does the exact same thing but it is interactive let's give it a full run-through add a new company what is the company name cool
company what is their phone number one two three one two three one two three four are you ready to insert cool company yes I am and we get an error looks like I made a typo here there we
go we'll fix that and one more time what is the company name cool company phone number are you ready to insert them yes and sure enough we added cool company so now
if we jump back to Chrome and we hit refresh we do have all of those companies that we've been working towards so that's pretty cool for a command so now you know how to
interactively create a command these are very useful whenever there's a lot of parameters that you have to bring in to be able to do your work because this way it guides the user through entering the
information as opposed to just asking them to kind of know what the command is all about so that makes them very user-friendly so in the next lesson I want to tackle a different kind of
command so when you're ready let's move on to the next lesson on closure based commands
Let’s start talking about how to create our very own artisan command. In part 2, let’s make our command interactive. Allowing users to be prompted with the required questions.
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!