Skip to content

Instantly share code, notes, and snippets.

@Yousha
Created November 5, 2024 08:36
Show Gist options
  • Save Yousha/2683f1976a0d711b80879bd24cecaddd to your computer and use it in GitHub Desktop.
Save Yousha/2683f1976a0d711b80879bd24cecaddd to your computer and use it in GitHub Desktop.
Laravel - clear all caches
<?php
/* 'composer dump-autoload',
'optimize:clear',
'queue:restart',
'clear-compiled',
'session:clear',
'tests-storage:clear',
'log:clear',
'event:clear',
'auth:clear-resets',
'debugbar:clear' */
# Laravel command
class ClearAllCaches extends Command implements Isolatable
{
// The name and signature of the console command.
protected $signature = 'clear-all-caches';
// The console command description.
protected $description = 'Clear all project caches and logs.';
// Execute the console command.
public function handle()
{
$this->info('Executing clear-all-caches command...');
// Note: `2>&1` tells the shell to redirect the standard error stream(2) to the same place as the standard output stream(1).
shell_exec('composer dump-autoload 2>&1');
// Laravel Artisan commands to call.
$_commands = [
'optimize:clear',
'queue:restart',
'clear-compiled',
'session:clear',
'tests-storage:clear',
'log:clear',
'event:clear',
'auth:clear-resets',
'debugbar:clear'
];
// Execute the commands using the Laravel Artisan facade.
foreach ($_commands as $command) {
Artisan::call($command);
}
$this->info('clear-all-caches execution has finished.');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment