How to Create Blade File in Laravel using CMD?
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, laravel 9, laravel 10 and laravel 11 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..
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Collection Check If Key Exists Example
- How to Convert Collection to JSON in Laravel?
- How to Select Specific Columns in Laravel Eloquent Model?
- Laravel 9 Many to Many Eloquent Relationship Tutorial
- How to Add Password Protection for PDF File in Laravel?
- Laravel Send Email with Multiple Attachment Example
- Laravel Livewire Delete Confirmation Example
- Laravel Next Previous Link Button Pagination
- How to Display Image from Storage Folder in Laravel?
- How to use Google ReCaptcha in Laravel?
- How to Execute Artisan Command from Controller in Laravel?
- How to Install Composer using Command Line in Ubuntu?
- Laravel Create Custom Artisan Command with Example
- How to Generate Dynamic Sitemap in Laravel?