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

By Hardik Savani August 18, 2022 Category : PHP Laravel Android

Hi Artisan,

In this tutorial, we will go over the demonstration of create blade file laravel command. I would like to show you laravel create view command. we will help you to give an example of laravel create blade command. This article will give you a simple example of create blade file laravel command. Here, Create a basic example of how to create blade file in laravel using command.

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

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

Without any further ado, let's see below code example.

Step 1: Install Laravel 9

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 custom artisan command using 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 able to create demo.blade.php blade file in resources/views directory using below command:

php artisan make:view demo

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

php artisan make:view demo.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:

I hope it can help you..

Tags :