
Making Command in Laravel is so easy just one artisan command and you are ready to rock. Laravel is elegant and powerful framework. If you are also want to make your own artisan command then this is guide for you where you will learn Make Your Own Artisan Command in Laravel.
Make Your Own Artisan Command in Laravel
As i said above you just one command to make your own command nothing else so look below and run in your Laravel project.
$ php artisan make:command StorageCount
Here I make a class with name StorageCount
you can replace with your own one.
Now you open folder -> app>Console>Commands and you will get your StorageCount file with some stub code.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class StorageCount extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
return 0;
}
}
Assign $signature value as per your command name if you pass only one value without colon then you will not get you own section and if you want to have your own section then you can pass value with colon.
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'storage:count
{path : Enter your folder name}
{--F|--folder : pass this is you want to count sub folders}';
Here we pass one argument (path
) and one option (--folder
) with description so whenever you run command php artisan storage:count -h
you can check details about argument and options.
Basic to Advance Routing in Laravel
If you want description for whole command then add your description in $description.
/**
* The console command description.
*
* @var string
*/
protected $description = 'Count files and folder from path';
You command will handle via handle() method for example we create some code that is laravel and php core method mixed.
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$dir = base_path($this->argument('path')) . "/";
$files = array_filter(glob($dir . "*"), "is_file") ?? 0;
if ($this->option('folder')) {
$folders = glob($dir . "*", GLOB_ONLYDIR) ?? 0;
$this
->info("Total: " . count($files) . " Files & " . count($folders) . " folders");
} else {
$this->info("Total: " . count($files) . " Files");
}
}
}
Thats all now you can easily count your files from any folder and folder also.
Lets put all together so you can see whole code.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class StorageCount extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'storage:count
{path : Enter your folder name}
{--F|--folder : pass this is you want to count sub folders}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Count files and folder from path';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$dir = base_path($this->argument('path')) . "/";
$files = array_filter(glob($dir . "*"), "is_file") ?? 0;
if ($this->option('folder')) {
$folders = glob($dir . "*", GLOB_ONLYDIR) ?? 0;
$this
->info("Total: " . count($files) . " Files & " . count($folders) . " folders");
} else {
$this->info("Total: " . count($files) . " Files");
}
}
}
Conclusion
That’s was easy right? Its so fun to make your own command and you can try by your own. Keep in mind above code not count dot files so if you was trying to count dot files and the command not counting files that’s is not your code problem. 😂