How to use clear cache command in Laravel controller

Many times in application development, you may need to clear application. For example, for testing purposes, when we are making changes, to avoid the affect of previous implementation we clear cache. In this tutorial, we are going to learn how to clear route cache, Laravel application cache, config cache in a Laravel 8 application inside a controller using with out artisan command-line interface.

public function clearAllCache() {


    return 'Route, Configuration and View cache has clear successfully!';

You can clear items from a Laravel application in many ways without the CLI.

Laravel Controller provides a space to call Artisan commands outside the CLI.

The call method can be used in a Laravel controller to call other commands from an existing Artisan command. The method works like a CLI interface. Therefore, it accepts a command name and an array of the command parameters.

The example above for the function clearAllCache() passes the command names to clear route, config, cache, and view data. However, no parameter is passed here. As it directs to clear all information.

In some cases, we [pass commands like email: send, as shown below. This is to send an email to a specific user. Therefore, we pass the specific user as a parameter here.

$this->call('email:send', [
        'user' => 1, '--queue' => 'default'

The methods above show how to use Artisan commands like the clear cache command in Laravel controller. To perform various function from the controller method and avoid using the CLI.

Leave a Comment