Skip to content

All topics  /  Laravel

How to Create Blade View File using Artisan Command in Laravel?

Laravel

Teams applying “How to Create Blade View File using Artisan Command in Laravel?” in a production project can use the relevant Monitask guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Sep 08, 2022

Hello Friends,

Today, I would like to show you how to create a blade view file using the artisan command in laravel. I explained simply about creating blade file laravel command. this example will help you how to create a view file in laravel using the artisan command. you can understand how to create a blade file in laravel using a command. Here, Creating a basic example of the laravel creates blade command example.

Laravel provides default artisan commands to create a model, controller, rule, etc. but you can not create a blade file using the artisan command. so we will create our own custom artisan command to create a blade file using it. we will create php artisan make:view {filename} to make blade file.

You can use this example with laravel 6, laravel 7, laravel 8, and laravel 9 versions.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel


This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Custom Artisan Command

Here, we will create a custom artisan command using the following command, so let's run it:

php artisan make:command MakeViewCommand

Next, let's update MakeViewCommand.php file with below code:

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 = 'Create a new blade template.';

  
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $view = $this->argument('view');

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

  
        $this->createDir($path);

  
        if (File::exists($path))
        {
            $this->error("File {$path} already exists!");
            return;
        }

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

  
        $this->info("File {$path} created.");
    }

  
     /**
     * Get the view full path.
     *
     * @param string $view
     *
     * @return string
     */
    public function viewPath($view)
    {
        $view = str_replace('.', '/', $view) . '.blade.php';

  
        $path = "resources/views/{$view}";

  
        return $path;
    }

  
    /**
     * Create view directory if not exists.
     *
     * @param $path
     */
    public function createDir($path)
    {
        $dir = dirname($path);

  
        if (!file_exists($dir))
        {
            mkdir($dir, 0777, true);
        }
    }
}

Step 3: Use Artisan Command

Now, you can use the following artisan command to create a blade file.

You will be able to create a demo.blade.php blade file in the resources/views directory using the below command:

php artisan make:view demo

You will be able to create a demo.blade.php blade file in resources/views/demo directory using the below command:

php artisan make:view demo

you can see below a preview of the newly created blade file.

resources/views/demo.blade.php

resources/views/demo.blade.php

You can see below terminal preview: