Artisan command: make scope

Artisan command: make


Add additional command to “php artisan make:…” console command list

 

NOTE:
I’m making custom global scope in this example, but it can be easily adjusted for any other template.
 
step 1: create Stubs directory (unless you have it already)

Location of this directory is up to you.
I located it in app\Console\, since this seemed logical to me.

step 2: inside of Stubs dir create your stub file

This is a file, that will be used as auto-generated Scopes file.

Here is what I use:

Please note:

  • DummyNamespace, and
  • DummyClass

… they will be replaced by names for class and namespace.

Both of Names will be deducted from your path, you put in your php artisan make command.

Besides pasting this stub file inside of Stubs dir, nothing needs to be done.

step 3: create console command class, that will actually make scope

Now we actually need to make command, that will take care of doing all the magic, after you open console window and type: php artisan make: MyScope

First, lest use existing Artisan make command and create command template file:

This will place out command template file in app\Console\Commands dir:

Now, you can either compare your newly created template with file I will paste right below and apply changes, or replace piecemeal all, that you have in scope template.

Actually, we did not have to use console command making scope template, but just create file by hand, name it MakeScope (or any other name, if you do not like it) and paste this code:

Code is pretty much self-explanatory – just read in-code comments.

Except for one thing – app disc creation.
Just put this code:

in Filesystem Disks in file: config/filesystems.php

step 4: now we have to register our command, so Artisan is able to find it and use it, when we type in command into console

We do it in Kernel – see below:

Inside of Kernel, just add line of code in method: protected $commands = [], see below:

How to use this is put on very top, but just to remind you:

I am not sure, if Laravel engine creates parent app\Scopes\ dir.

In case it does not (and you get some errors, or nothing happens), add it by hand here:

Unless I missed something, above step-by-step should get you working make shortcut accessible from Artisan console.