Laravel Custom Command Make Example

  • 28-09-2022
  • 725
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

How to create a custom command in laravel, In this post we will create a custom command in our laravel application. We create a command for the create blade view file using the make:view command. For that, we need to do some coding in the handle() method generated command file.

Laravel already provides all the commands, and also provides custom command-make functionality. So You can make yourself a command in the application according to your requirements. Having ended this post example you will be able to create custom commands in your application according to your requirement.

This is very simple to create a laravel custom command, follow the given guideline and done in your application. So let's start.

Make Command

Firstly, we will create a command file in the laravel application in the default command file. The command file will create with the basic structure. The file will be generated in the App\Console\Commands\MakeViewCommand.PHP, Use the below command in your terminal and create a command file.

php artisan make:command MakeViewCommand

Make View Command

Now, we will define the command name in the protected signature variable. Extra things like parameters you want to pass in a command, also need to define in command as below example defined.

Also add some command descriptions in the file protected description variable. The construct method will be run automatically.

In the handle() method whatever you want to do activities with the command, We will code in the handle method by using the command want to take action. In this example, I am creating a blade view file using the command.

App\Console\Commands\MakeViewCommand.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use File;

class MakeViewCommand extends Command
{
    /**
    * The name and signature of the console command.
    *
    * @var string
    */

    protected $signature = 'make:view {view}';

    /**
    * The console command description.
    *
    * @var string
    */

    protected $description = 'Blade File Created Successfully.';

    /**
    * Create a new command instance.
    *
    * @return void
    */

    public function __construct()
    {
        parent::__construct();
    }

    /**
    * Execute the console command.
    *
    * @return int
    */

    public function viewPath($view)
    {
        $view = str_replace('.','/',$view). '.blade.php';
        
        $path = 'resources/views/'.$view;

        return $path;
    }

    public function createDir($path)
    {
        $dir = dirname($path);

        if (! file_exists($dir)) {

            mkdir($dir,0777,true);
        }
    }

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

        $path = $this->viewPath($view);

        $this->createDir($path);

        if (File::exists($path)) {

            $this->error('File In '.$path.' already exists.');

            return;
        }

        File::put($path,$path);

        $this->info('File In '.$path.' Created.');
    }
}

Now all the setup is done, Open your laravel application terminal and run the below-suggested command in your terminal.

php artisan make:view front/home

Laravel Most Useful Command's Lists With Parameters Example


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :