DEV Community

Arman Rahman
Arman Rahman

Posted on

Create Custom Artisan Command in Laravel

Sometimes we need to create custom commands to fast-forward our tasks. So here is the process to create your custom command for your Laravel project.

First Create a command class with this command -

php artisan make:command CustomCommand
Enter fullscreen mode Exit fullscreen mode

After that, it will create a file on this path -

app\Console\Commands\CustomCommand.php

Then Update your custom command like this -

protected $signature = 'make:files {name}';
Enter fullscreen mode Exit fullscreen mode

Add your command description and update your custom conditions on handle() method.

Here is added my conditions -

public function handle()
    {
        $name = $this->argument('name');

        $this->call('make:model', [
            'name' => $name,
            '-m' => true,
        ]);

        $this->call('make:controller', [
            'name' => "{$name}Controller",
        ]);

        $this->call('make:request', [
            'name' => "{$name}/{$name}StoreRequest",
        ]);

        $this->call('make:request', [
            'name' => "{$name}/{$name}UpdateRequest",
        ]);

        $this->info("Resource files for {$name} have been generated.");

        return Command::SUCCESS;
    }
Enter fullscreen mode Exit fullscreen mode

Done You have successfully created your command for your project.

Top comments (0)