Clearing Cache in Laravel 8/9/10

In this tutorial, we will learn how to clear the cache using code as well as using commands via the terminal in a Laravel application.

Clearing Cache using Code

To clear the cache in Laravel using code, you can create a function as shown in the example below:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Artisan;

class UserController extends Controller
{

    public function clearCache()
    {
       Artisan::call('cache:clear');
    }
}

Clearing Configuration Cache

To clear the Laravel configuration cache, navigate to the application's root folder and execute the following command via the terminal:

php artisan config:clear

Clearing View Cache

To clear the Laravel view cache, you can navigate to the application's root folder and execute the following command via the terminal:

php artisan view:clear

Clearing Route Cache

To clear the Laravel route cache, you can navigate to the application's root folder and execute the following command via the terminal:

php artisan route:clear

Clearing Application Cache

To clear the Laravel application cache, navigate to the application's root folder and run the following command via the terminal:

php artisan cache:clear