<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: KenFai</title>
    <description>The latest articles on Forem by KenFai (@kenfai).</description>
    <link>https://forem.com/kenfai</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F343345%2F348b67ef-7664-49a6-9bdb-41266999bb81.jpeg</url>
      <title>Forem: KenFai</title>
      <link>https://forem.com/kenfai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kenfai"/>
    <language>en</language>
    <item>
      <title>Laravel Artisan Cache Commands Explained</title>
      <dc:creator>KenFai</dc:creator>
      <pubDate>Sun, 08 Nov 2020 14:08:40 +0000</pubDate>
      <link>https://forem.com/kenfai/laravel-artisan-cache-commands-explained-41e1</link>
      <guid>https://forem.com/kenfai/laravel-artisan-cache-commands-explained-41e1</guid>
      <description>&lt;p&gt;Often times, when you are in the middle of developing a Laravel application, you may find that the changes you made in your code are not reflecting well on the application when testing.&lt;/p&gt;

&lt;p&gt;Usually, the case is most likely caused by caching applied by the Laravel framework.&lt;/p&gt;

&lt;p&gt;Here are some of the common commands you can run in your terminal to alleviate the issue.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;❗️ Make sure you are running them in the context of your application. Meaning, your terminal is currently in the same directory as your Laravel application.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. Configuration Cache
&lt;/h2&gt;

&lt;p&gt;Caching configuration helps with combining all of the configuration options for your application into a single file which will be loaded quickly by the framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clearing Configuration Cache
&lt;/h3&gt;

&lt;p&gt;However, if you notice changes to the configuration values in &lt;code&gt;.env&lt;/code&gt; file is not reflecting on your application, you may want to consider clearing the configuration cache with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan config:clear
Configuration cache cleared!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to quickly reset your configuration cache after clearing them, you may instead run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Caching your configuration will also help clear the current configuration cache. So it helps save your time without having to run both commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Route Caching
&lt;/h2&gt;

&lt;p&gt;Caching your routes will drastically decrease the amount of time it takes to register all of your application's routes. When you add a new route, you will have to clear your route cache for the new route to take effect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clearing Route Cache
&lt;/h3&gt;

&lt;p&gt;The following command will clear all route cache in your application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan route:clear
Route cache cleared!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To cache your routes again, simply run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan route:cache
Route cache cleared!
Routes cached successfully!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, running the above command alone is enough to clear your previous route cache and rebuild a new one.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Views Caching
&lt;/h2&gt;

&lt;p&gt;Views are cached into compiled views to increase performance when a request is made. By default, Laravel will determine if the uncompiled view has been modified more recently than the compiled view, before deciding if it should recompile the view.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clearing View Cache
&lt;/h3&gt;

&lt;p&gt;However, if for some reason your views are not reflecting recent changes, you may run the following command to clear all compiled views cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan view:clear
Compiled views cleared!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, Laravel also provides an Artisan command to precompile all of the views utilized by your application. Similarly, the command also clears the view cache before recompiling a new set of views:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan view:cache
Compiled views cleared!
Blade templates cached successfully!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Events Cache
&lt;/h2&gt;

&lt;p&gt;If you are using Events in your Laravel application, it is recommended to cache your Events, as you likely do not want the framework to scan all of your listeners on every request.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clearing Events Cache
&lt;/h3&gt;

&lt;p&gt;When you want to clear your cached Events, you may run the following Artisan command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan event:clear
Cached events cleared!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Likewise, caching your Events also clear any existing cache in the framework before a new cache is rebuilt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan event:cache
Cached events cleared!
Events cached successfully!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Application Cache
&lt;/h2&gt;

&lt;p&gt;Using Laravel's Cache is a great way to speed up frequently accessed data in your application. While developing your application involving cache, it is important to know how to flush all cache correctly to test if your cache is working properly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clearing Application Cache
&lt;/h3&gt;

&lt;p&gt;To clear your application cache, you may run the following Artisan command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan cache:clear
Application cache cleared!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will clear all the cache data in storage which are typically stored in &lt;code&gt;/storage/framework/cache/data/&lt;/code&gt;. The effect is similar to calling the &lt;code&gt;Cache::flush();&lt;/code&gt; Facade method via code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;❗️ This command will NOT clear any &lt;strong&gt;config&lt;/strong&gt;, &lt;strong&gt;route&lt;/strong&gt;, or &lt;strong&gt;view&lt;/strong&gt; cache, which are stored in &lt;code&gt;/bootstrap/cache/&lt;/code&gt; directory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  6. Clearing All Cache
&lt;/h2&gt;

&lt;p&gt;Laravel provides a handy Artisan command that helps clear &lt;em&gt;ALL&lt;/em&gt; the above caches that we have covered above. It is a convenient way to reset all cache in your application, without having to run multiple commands introduced before.&lt;/p&gt;

&lt;p&gt;To clear all Laravel's cache, just run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ 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!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can read from the terminal feedback, all cache types that existed in your Laravel application will be cleared entirely, except Events cache.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;❗️ The &lt;code&gt;Compiled services and packages files removed!&lt;/code&gt; can be run individually via &lt;code&gt;$ php artisan clear-compiled&lt;/code&gt; command. It is used to remove the compiled class file in the framework.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  🎁 Bonus
&lt;/h2&gt;

&lt;p&gt;If the above Laravel's Artisan commands don't seem to resolve the issue you are facing, you may need to look at other related environments in your project that may be causing it.&lt;/p&gt;

&lt;p&gt;When building a Laravel project, it is common to employ the Composer Dependency Manager for PHP, as well as NPM for any JavaScript library that might be needed in your project. We just have to take note that both package managers are using some form of caching for performance improvements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clearing Composer Cache
&lt;/h3&gt;

&lt;p&gt;Sometimes, a new package you just installed via Composer doesn't appear to be working at all. Or a new project you just cloned from a repository doesn't seem to be running correctly.&lt;/p&gt;

&lt;p&gt;Such issues are usually caused by classmap error from a newly installed library class, or the cached version of a particular library does not match with the ones required by the project codebase you just cloned. In such a situation, you need to update the PHP autoloader by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ composer dump-autoload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As well as any of the following variations(they all achieve the same purpose of deleting all content from Composer's cache directories):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ composer clear-cache
$ composer clearcache
$ composer cc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Clearing NPM Cache
&lt;/h3&gt;

&lt;p&gt;To clear NPM cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm cache clean --force
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🏁 Summary
&lt;/h2&gt;

&lt;p&gt;That's all you need to know about Laravel's Artisan cache related commands, as well as few additional commands for Composer and NPM, which you most likely will be using together in a Laravel project.&lt;/p&gt;

&lt;p&gt;Here's a final command before we end the article:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ php artisan list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command will list down all available Artisan commands when executed. I recommend you to take a look at it, as you might find something new that will be useful to you!&lt;/p&gt;

&lt;p&gt;❗️ Thank you for reading! If you find this useful, feel free to share it with your followers. 🦊&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://laravel.com/docs/6.x/configuration#configuration-caching"&gt;https://laravel.com/docs/6.x/configuration#configuration-caching&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laravel.com/docs/8.x/controllers#route-caching"&gt;https://laravel.com/docs/8.x/controllers#route-caching&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laravel.com/docs/8.x/views#optimizing-views"&gt;https://laravel.com/docs/8.x/views#optimizing-views&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laravel.com/docs/8.x/events#event-discovery"&gt;https://laravel.com/docs/8.x/events#event-discovery&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laravel.com/docs/8.x/cache#removing-items-from-the-cache"&gt;https://laravel.com/docs/8.x/cache#removing-items-from-the-cache&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://getcomposer.org/doc/03-cli.md#dump-autoload-dumpautoload-"&gt;https://getcomposer.org/doc/03-cli.md#dump-autoload-dumpautoload-&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://getcomposer.org/doc/03-cli.md#clear-cache-clearcache-cc"&gt;https://getcomposer.org/doc/03-cli.md#clear-cache-clearcache-cc&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.npmjs.com/cli/v6/commands/npm-cache"&gt;https://docs.npmjs.com/cli/v6/commands/npm-cache&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>cache</category>
      <category>composer</category>
    </item>
  </channel>
</rss>
