Laravel 9 Create Blade View File Using Artisan Command

Reading Time: 5 minutes
2,279 Views

Inside this article we will see the concept i.e Laravel 9 Create Blade View File Using Artisan Command Tutorial. Article contains the classified information about How to create blade file in laravel using a command.

If you are looking for a solution i.e create blade file via laravel artisan command then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Laravel artisan provides a command palette to operate with laravel application. It is a command line tool which gives application access by means of commands. We can create like controllers, components, models, migrations etc.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Custom Command (View)

Open project into terminal and type this artisan command to see all available commands.

$ php artisan

This command will list all available commands in artisan panel.

To create blade file, artisan doesn’t has any comment for this. So, we will create a custom command for it.

To create custom command in laravel, we use make:command artisan command. Back to terminal and run this command:

$ php artisan make:command MakeViewCommand

Above command will create a file MakeViewCommand.php inside /app/Console/Commands folder.

Open MakeViewCommand.php and write this complete code into it.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\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, "Haha, Blade template file created.");

        $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);
        }
    }
}

This will create a custom command into laravel artisan panel.

Generate View File (Using Custom Command)

To create a view file, back to project terminal and run this command.

Create Blade File

$ php artisan make:view demo

It will create a blade template file with name demo.blade.php inside /resources/views folder. Message will be like:

File resources/views/demo.blade.php created.

Once, file will be created then again if try the same command. Message will be like:

File resources/views/demo.blade.php already exists!

Create Blade File Inside Folder

Run this command –

$ php artisan make:view site.demo

It will create a blade template file with name demo.blade.php inside /resources/views/site folder.

Inside each generated view file, you will see the content as:

Haha, Blade template file created.

We hope this article helped you to learn about Laravel 9 Create Blade View File Using Artisan Command Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.