Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e36 – Artisan Commands (Closure) – Part 3

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

Laravel 5.8 Tutorial From Scratch – e36 – Artisan Commands (Closure) – Part 3

welcome back this is part three of artisan commands in this episode we're going to be talking about the closure based commands available in laravel now

in the previous two episodes we work in a full-fledged class that was a console command however levo offers another convenient way of having commands and that is through a closure for very

simple commands this approach is quite good so let me show you how to find the file first if we jump into our routes directory we've worked with web and we've worked with API but there's

another one here for console and this allows us to define a closure based console command so this is pretty cool check this out so there is a command in here already

and that is in spire let's run that now and see what it does PHP artisan in spire it always seems impossible until it is done so this command just picks out a quote and sends

it back to the user pretty cool command so it's actually using this inspire class and just calling the quote on it and if we look at this inspired command we see there's

a handful of commands in here that it chooses from and then it's calling the random method on it so all we're getting is a random quote from someone pretty cool command this is a very simple

command and obviously this is a great use for it let's add our own now I want to make a simple command to clean out any companies that are not being used in our project so we're gonna fetch any

company that's not being used and we're gonna delete that record so how can we handle that we'll say artisan command and what's our command going to be well contact is our prefix

so we'll say contact company – clean and then as a second parameter you have to pass in a closure now in here this is no different than the handle method from our actual command so I'm gonna say just

for now this info and I will just say cleaning command that's a deep alright there we go alright so let's run PHP artisan and if we scroll up we do have this second command right here now to

add the description we can pass that to the end of this and just say describe cleans up on used companies alright so now if we run PHP artisan again

cleans up unused companies pretty cool so we have added a command inline using a closure so how can we fetch all of the unused companies so check this out we could say company and that is being

imported here at the top use app company if you don't add that in then you will get a company not found exception so we'll say company where doesn't have and what is the relationship that we are

looking for where the company doesn't have any customers remember if we go to the company model remember that company has customers so this is the relationship that we are using so

companies where doesn't have any customers and let's just get them for now I will die and dump that I just want to show you what it actually outputs let's run PHP artisan contact company

clean and sure enough we get a list of companies nine to be exact now obviously eight because it is zero based index so none of these have any customers so we can safely delete all of them so now

let's chain everything together and do the command in one big swoop so we'll say get them and then each one what I want you to do is run them through this closure we're gonna be

receiving a company right here and we can say company delete this and then maybe we'll use the warning delete it and then let's pass in that company's name that way we know what we actually

delete it does that make sense we're gonna grab the company model and then where it doesn't have any customers then we're going to get that so at this stage what we have is a collection of

companies that don't have any customers then we're going to take each one and we're going to run them through this closure now this closure it's going to receive each of those companies as a

very then we're going to call the delete method on it and then we're simply going to output something back to the user so they know that we actually completed

something great let's try it out I'm gonna run that one more time that's PHP artisan contact company clean alright and sure enough we delete a de F company

a test company new company a bunch of banana companies and then cool company now if we run that again then of course we don't get anything if we head back to our browser hit refresh we successfully

cleaned up our companies obviously there was only one company ABC company here so let's add a new company so what is the company name new company phone number yes alright so now we have this new

company so let's add a new customer email at email com and let's make sure that we use that new company so we added that customer and of course now new company is being used so if we go back

and try to do a clean up we don't clean anything because both of those companies are correct so I will actually go ahead and delete this record so now there are no customers associated with that second

company and I will run our command one more time and sure enough we deleted it because there was nothing associated with it so let's recap everything that we took care of if we go back to

phpstorm we made a new artisan command but this time we made it inside the console routes file first we went to routes and we found that console dot PHP we did

explore this inspire command which all it does is it randomly generates a quote and gives it back to the user but then we added our own artisan command using this signature right here and what it

does is it goes through our companies and anything that does not have a customer associated to it we're going to take each one of those and we're going to delete them and then we're going to

output out something back to the user so that we know exactly what we did now this is a cool little command and it joined our other command that we already had made if we go to PHP artisan

we do see that we have two commands now that we've put together this first one of course is a class-based command and this second one is a closure based command now there's a good use case for

either one but I think that you get the point for a simple command something like this works great if you have a command that is a little bit more robust and it's got a lot of logic in it

probably its own dedicated class will be a better place for it this command here as an inline command is kind of already pushing the boundaries I think of what should be a closure based command it's

got about ten lines of code in it and it's getting a little big so anything bigger than this should probably go in its own class now this is another place for you to be able to experiment with a

command this is a great way for you to dive into commands and start using everything in the command and then when you're ready you can refactor it to its own class so it's a great place to

experiment so keep that in mind whenever you're trying to find a little playground for you to experiment with some stuff this is a great place for you to do it so with that we've covered all

of the basics of artists and commands as always if you have any questions drop them in the comments below and I'll try to answer those for you and so with that in the next lesson we'll move on to

something new

Let’s start talking about how to create our very own artisan command. In part 3, let’s add a command to clean up any used companies in our database. We will do this by adding a closure-based command in the routes file.

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