Create dynamic view in Laravel 10 , create custom artisan command using Laravel 10

Create dynamic view in Laravel 10 , create custom artisan command using Laravel 10

08-Nov-2023
| |
Image Carousel

Hello developers in this tutorial we will discuss about how to create custome artisan command and how to create dynamic view in laravel 

Table of Contents

S.no Contents-topics
1 What are Artisan commands
2 Create custom artisan command for view
3 Code for command view file
4 Entering artisan command

1:What are Artisan commands

Artisan commands are similar to commands on CMD or commands which we enter in terminal to make changes , Laravel comes with inbuilt command line interface that name is Artisan , by the use artisan command we can make jobs , model , controller , migration and many more with the artisan command

2:Create new artisan command for view

For creating new custom artisan for creating view  , simply open the terminal and hit the below command with respective to file name.
Copy for creating artisan commandCopy

php artisan make:command createViewFile

Inside app/console/Commands/  you can see createViewFile.php 

3:Code for command view file

Also read: AUTH GUARD IN LARAVEL 10 , MAKE MODEL AUTHENTICABLE IN LARAVEL 10

createViewFile.php code: Copy

<?php
 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use File;
 
class createViewFile 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 view file';
 
    /**
     * Execute the console command.
     */
    public function handle()
    {
        $viewname = $this->argument('view');
 
        $viewname = $viewname.'.blade.php';
 
        $pathname = "resources/views/{$viewname}";
 
        if(File::exists($pathname)){
            $this->error("file {$pathname} is already exist " );
            return;
        }
        $dir = dirname($pathname);
        if(!file_exists($dir))
        {
          mkdir($dir,0777,true);
        }
 
        $content = '<!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Document</title>
        </head>
        <body>
           
        </body>
        </html>';
 
        File::put($pathname , $content);
 
        $this->info("File {$pathname} is created");
       
    }
}

Here  protected $signature = 'make:view {view}';  represents how to start this command and {view} means what should be name of our view file after that in handle() function consist of main logic behind the command to be executable.
 

4:Entering artisan command

command:  Copy

php artisan make:view front/aboutUs

Tags: create dynamic view in laravel 10 ,create custom artisan command using laravel 10 , laravel 10 advance features , custom artisan command , artisan command in laravel , create custom artisan command,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter