Table of Contents

Laravel Artisan

Laravel Artisan

A zombie developer creates files manually, whereas a Laravel developer uses artisan to help generate files and functionality for them.

When fighting a zombie would you rather have a shotgun or an axe?

Probably a shotgun, it would make killing zombies much faster easier. Well, if there was a weapon or tool for creating your app faster and easier wouldn't you want to use it?

That's exactly what Laravel's artisan command provides for us.


Artisan

We've used the artisan command back in section 2 to start up a local server. Remember, we ran the command php artisan serve in our laravel project folder via command line to start a local development server of our laraval application.

A quick definition of artisan is that it is a command line tool that can help generate files and run php commands.

If you run php artisan in a command prompt, you will see a list of available commands available to use.

In this section, we are only going to go over a few basic commands to get you started. First let's talk about how you can create new files.

Making new files with Artisan

Using the php artisan make command we can generate files that we need for our app.

In the last section and back in section 5 when we needed a new Controller we just created a new file and started adding our code. Well, instead of manually creating the file we could have run an artisan command to do this for us:

$ php artisan make:controller ZombieController

And if we look inside of our App\Http\Controllers folder we will see a new file called ZombieController.php that has the following code inside of it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ZombieController extends Controller
{
    //
}

How great is that! We just ran one command, and our controller is ready for us to start adding methods!

We could also have used a similar command to create our Zombie Model, like so:

$ php artisan make:model Zombie

And now if we look inside our app folder we will see a new file called Zombie.php with the following contents:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Zombie extends Model
{
    //
}

And just like that we've created our model class that can interact with our zombies table in our database.

A> Quick side note: if you want this Model to interact with another database table you could always add a protected variable called $table, like so protected $table = 'zombie_folks'; and now this model will interact with the zombie_folks table in your database.

It's amazing how much easier our Laravel programming will be if we leverage the power of artisan.

In this section we only covered 2 basic uses of the artisan command php artisan make:controller & php artisan make:model, but there are so many more artisan commands for you to master and add to your arsenal of helpers as you program the next greatest app.

Be sure to head on over to https://laravel.com/docs/artisan to learn all about artisan and all the other commands you can use.

In the next section, we are going to cover middleware, which is a great way to run code between page requests.