When you're working on a Laravel application, you could notice that the changes you made in your code aren't reflected well in the application when you test it. Most of the time, the situation is caused by the Laravel framework's caching.
Here are some typical commands that you may use in your terminal to solve the problem.
Ensure that you're running them in the context of your app. That is, your terminal is in the same directory as your Laravel application right now.
1. Configuring Cache
Setting up the system Cache Caching configuration allows you to combine all of your application's configuration choices into a single file that the framework can load rapidly.
Getting Rid Of The Configuration Cache
If you find that changes to the configuration settings in the .env file ain't reflects in your application, try emptying the configuration cache with the following command:
$ php artisan config:clear
Configuration cache cleared!You may alternatively use the following command to reset your configuration cache after clearing it rapidly:
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!Clearing the current configuration cache is also aided by caching your settings. As a result, you can save time by not having to perform both instructions.
2. Caching Of Routes
The time it takes to register will substantially reduce all of your application's routes if you cache your routes. You must delete your route-cache after adding a new path for it to take effect.
Clear Route Cache
The following command will clear your application's route cache:
$ php artisan route:clear
Route cache cleared!Execute the following command to re-cache your routes:
$ php artisan route:cache
Route cache cleared!
Routes cached successfully!Running the above command by itself will delete your previous route cache and create a new one.
3. Caching Of Views
When making a request, views are cached into compiled views to improve speed before determining whether or not to recompile the view. Laravel checks to see if the uncompiled statement has been updated more recently than the compiled view.
View Cache Clearing
However, if your views are not reflecting current changes, you may clear all built ideas Cache by running the following command:
$ php artisan view:clear
Compiled views cleared!Laravel also has an Artisan tool that allows you to precompile all of your application's views. Similarly, before recompiling a fresh set of views, the command clears the view cache:
$ php artisan view:cache
Compiled views cleared!
Blade templates cached successfully!4. Cache Of Events
If you use Events in your Laravel application, you should cache them since you don't want the framework to check all of your listeners on every request.
Clearing The Cache Of Events
You may clear your cached Events by running the following Artisan command:
Similarly, caching your Events clears any existing cache in the framework before rebuilding a new cache:
$ php artisan event:clear
Cached events cleared!5. Cache For Application
Using Laravel's Cache to speed up frequently accessed data in your application is a great approach to improve performance. When creating an application that uses Cache, it is critical to properly flush all Cache to test if your Cache is operating correctly.
Clearing The Application Cache
You may clear your application cache by running the following Artisan command:
$ php artisan cache:clear
Application cache cleared!It will clear all cache data from storage, which generally keeps at /storage/framework/cache/data/. The result is the same as using the Cache::flush(); Facade function from code.
Note: This command will NOT clear any config, route, or view caches saved in the /bootstrap/cache/ directory.
6. Clearing All Cache
Laravel has a useful Artisan tool that allows you to delete ALL of the caches mentioned above. It is a handy approach to clear all Cache in your program without executing the previous instructions.
Execute the following command to delete all of Laravel's Cache:
$ php artisan optimize:clear
Compiled views cleared!
Application cache cleared!
Route cache cleared!
Configuration cache cleared!
Compiled services and packages files removed!
Caches cleared successfully!As you can see from the terminal feedback, all cache types in your Laravel application have been completely deleted, except the Events cache.
Note: The compiled services and packages files have been deleted! It may execute independently using the $ PHP artisan clear-compiled command. It is used to delete the framework's collected class file.
Composer Cache Cleaning
Sometimes a new package you installed using Composer doesn't seem to be operating at all. Or maybe a new project you recently cloned from a repository isn't working correctly.
Such problems typically trigger a class-map error from a freshly installed library class or when the cached version of a particular library does not match the ones required by the project codebase you just cloned.
In this case, you must update the PHP autoloader by running the following command:
$ composer dump-autoloadAny of the following variants (all of which achieve the same goal of removing all content from Composer's cache directories):
$ composer clear-cache
$ composer clearcache
$ composer ccClearing of the NPM Cache
To clear the NPM cache, follow these steps:
$ npm cache clean --forceSummary
That's all you need to know about Laravel's Artisan cache-related commands, as well as a few more Composer and NPM tasks that you'll most likely use in a Laravel project.
Before we conclude, here's one more command:
$ php artisan listWhen performing the above command, it will display a list of all possible Artisan commands. I urge you to look at it since you could learn something new that will benefit you!
Thanks this article help me to solve my issue of how to create custom helper in laravel.