Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







Laravel Clear Cache Of Route, View, Config Command | StackCoder


Laravel Clear Cache Of Route, View, Config Command


Share On     Share On WhatsApp     Share On LinkedIn


Most of the time you may end up refreshing your view pages or adding new routes but it won't be accessible from the user browser. It's all because of caching that Laravel does for us.


In this article let's see how to clear Laravel application cache, routes, views & configs caches in a very simple way.


Topics that we will cover


  1. A simple command in Laravel 7 & Above
  2. Application Cache Clearing
  3. Route Cache Clearing
  4. View Cache Clearing
  5. Config Cache Clearing
  6. Clear All Caches Using Simple Callback URL
  7. Clear All Caches Using Custom Command

Single Command In Laravel 7 & Above


In Laravel 8 you can run the following command and breath easily.

php artisan optimize:clear

Laravel 6 & below


Application Cache Clearing


Flush the application cache

php artisan cache:clear

Route Cache Clearing


Remove the route cache file

php artisan route:clear

View Cache Clearing


Clear all compiled view files

php artisan view:clear

Config Cache Clearing


Remove the configuration cache file

php artisan config:clear

Clear All Caches Using Simple Callback URL In web.php


We can create a simple callback URL in web.php to clear all the caches at a stretch

Route::get('/clear-all-cache', function() {
  Artisan::call('cache:clear');
  Artisan::call('route:clear');
  Artisan::call('view:clear');
  Artisan::call('config:clear');
  echo "Cleared all caches successfully.";
});

Clear All Caches Using Custom Command


Let's create a simple command utility so that we can use it as and when required from the command line.


Create ClearAllCache Command

php artisan make:command ClearAllCache


ClearAllCache.php command class

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class ClearAllCache extends Command
{
    protected $signature = 'cache:clear-all';

    protected $description = 'Command to clear application, route, view & config caches';
    
    public function handle()
    {
        Artisan::call('cache:clear');
        Artisan::call('route:clear');
        Artisan::call('view:clear');
        Artisan::call('config:clear');
        echo "Cleared all caches successfully.";
    }
}


You can verify the above command using the following in the console


php artisan

Conclusion


I hope this article helped you. Please share it with your friends.




Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing