Laravel 9 Call Artisan Command From Controller Tutorial

Reading Time: 3 minutes
1,698 Views

Inside this article we will see the concept i.e Laravel 9 Call Artisan Command From Controller Tutorial. Article contains the classified information about Call Artisan Command from Controller in Laravel.

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

This article is not version specific, you can use use it with Laravel v6, v7, v8, v9.

Read More: PHP How to Remove Numbers From String Tutorial

The Laravel PHP artisan serve command helps running applications on the PHP development server. As a developer, you can use Laravel artisan serve to develop and test various functions within the application.

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 Controller

Back to project terminal and run this command to create a controller file.

$ php artisan make:controller SiteController

Above command will create a file i.e SiteController.php inside /app/Http/Controllers folder.

Read More: Laravel 9 How To Get All Application Routes Tutorial

Open SiteController.php and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function clearCache()
    {
        /* php artisan cache:clear */
        \Artisan::call('cache:clear');

        dd('Application cache cleared successfully.');
    }

    public function clearViews()
    {
        /* php artisan view:clear */
        \Artisan::call('view:clear');

        dd('Compiled views cleared successfully.');
    }
}

Create Route

Open web.php file from /routes folder. Add these routes into it.

//...

use App\Http\Controllers\SiteController;

Route::get("clear-cache", [SiteController::class, "clearCache"]);
Route::get("clear-views", [SiteController::class, "clearViews"]);

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

To clear application’s cache

URL: http://127.0.0.1:8000/clear-cache

Read More: Laravel 9 Redirection with Form Inputs Example Tutorial

To clear application’s compiled views

URL: http://127.0.0.1:8000/clear-views

We hope this article helped you to learn Laravel 9 Call Artisan Command From Controller 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.