There are several ways to clear cache in Laravel 8 via routes, controllers, and commands. Here are three possible methods:
- Using a Route: Create a new route in your
web.php
file that will clear the cache when accessed:
Route::get('/clear-cache', function() {
Artisan::call('cache:clear');
return "Cache is cleared";
});
This route will call the cache:clear
command provided by Artisan, which will clear all cached data.
- Using a Controller: Create a new controller method that will clear the cache when called:
use Illuminate\Support\Facades\Artisan;
public function clearCache()
{
Artisan::call('cache:clear');
return "Cache is cleared";
}
Then, define a new route in your web.php
file that will call this controller method:
Route::get('/clear-cache', [YourController::class, 'clearCache']);
Using a Command:
Create a new command that will clear the cache when executed:
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
class ClearCacheCommand extends Command
{
protected $signature = 'cache:clear-custom';
protected $description = 'Clear the application cache';
public function handle()
{
Cache::flush();
$this->info('Application cache cleared!');
}
}
Then, you can run this command in your terminal using php artisan cache:clear-custom
.
Note that all three of these methods will clear the application cache, including the cache for views, configurations, and routes.