<?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>DEV Community: Shashika Lakshitha</title>
    <description>The latest articles on DEV Community by Shashika Lakshitha (@slshashika).</description>
    <link>https://dev.to/slshashika</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%2F296266%2F554f0299-2317-43a8-98f3-874f0127c0c0.jpg</url>
      <title>DEV Community: Shashika Lakshitha</title>
      <link>https://dev.to/slshashika</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/slshashika"/>
    <language>en</language>
    <item>
      <title>Understanding the Difference: Laravel Lazy Loading vs Eager Loading</title>
      <dc:creator>Shashika Lakshitha</dc:creator>
      <pubDate>Sun, 16 Feb 2025 11:00:55 +0000</pubDate>
      <link>https://dev.to/slshashika/understanding-the-difference-laravel-lazy-loading-vs-eager-loading-5hn9</link>
      <guid>https://dev.to/slshashika/understanding-the-difference-laravel-lazy-loading-vs-eager-loading-5hn9</guid>
      <description>&lt;p&gt;Lazy loading and Eager loading are two different strategies used to load the data from the database. They are used to optimize the performance of laravel application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lazy Loading
&lt;/h2&gt;

&lt;p&gt;Lazy Loading is a technique where related data is loaded from the database only when it is accessed. This means that the related eloquent model's data is not retrieved until the relationship is explicitly accessed in the code. Eloquent Relationships must be defined in the models for lazy loading to work. Eloquent relationships are defined as methods on your Eloquent model classes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works…&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you query the main model, Laravel retrieves only the main data from the database. If you need related data, a separate query is executed when you access the relationship.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$author = Author::find(10); // This fetches the specific author data.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, only the author's data is fetched. If you want to fetch the books related to that author, you need to access the relationship which is defined in the model&lt;/p&gt;

&lt;p&gt;Eloquent relationship example in Author.php - Model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 // Define the relationship an author has many books
    public function books()
    {
        return $this-&amp;gt;hasMany(Book::class);
    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Access the relationship 
 $books = $author-&amp;gt;books; //  new query to fetch the books for that author.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you retrieve a model, the related models are not loaded by default, only when the related relationship in accessed, the query is executed to fetch the related data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The N+1 Query Problem&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lazy loading can lead to the N+1 query problem when you loop through multiple parent models and access their relationships.&lt;/p&gt;

&lt;p&gt;Lazy loading is most suitable in scenarios where the related data is not always needed. Otherwise, it can lead to performance issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Eager Loading
&lt;/h2&gt;

&lt;p&gt;Eager loading is a technique where related data models are loaded simultaneously with the main model. This means that when user data is fetched, the related user data is also retrieved in a single query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works…&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = User::with(‘post’)-&amp;gt;find(10);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Laravel provides eager loading using the &lt;code&gt;with()&lt;/code&gt; method. This method loads related model data efficiently. This can reduce the number of executed queries. It helps improve performance and prevents the N+1 query problem.&lt;/p&gt;

&lt;p&gt;This can increase the initial load time if too much related data is loaded at once. It also consumes more memory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;However, both methods are necessary. When choosing between eager loading and lazy loading, consider the specific use case and the performance requirements of the web application.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>performance</category>
      <category>backend</category>
    </item>
    <item>
      <title>How to clear the Laravel cache?</title>
      <dc:creator>Shashika Lakshitha</dc:creator>
      <pubDate>Sun, 04 Aug 2024 07:41:23 +0000</pubDate>
      <link>https://dev.to/slshashika/how-to-clear-the-laravel-cache-4l1e</link>
      <guid>https://dev.to/slshashika/how-to-clear-the-laravel-cache-4l1e</guid>
      <description>&lt;p&gt;Laravel, a popular PHP web application framework, offers powerful caching mechanisms to enhance the performance of your application. However, there are situations where you might need to clear the cache, especially during development or after making configuration changes. In this guide, we'll explore the step-by-step process of clearing various caches in Laravel&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Configuration Cache:&lt;/strong&gt;&lt;br&gt;
One of the primary caches you might want to clear is the configuration cache. This cache stores the configuration files in a compiled and optimized form. To clear it, open your terminal and run the following Artisan command:&lt;br&gt;
&lt;code&gt;php artisan config:cache&lt;/code&gt;&lt;br&gt;
This command will clear the configuration cache and recompile the configuration files, ensuring that any changes you've made take effect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Route Cache:&lt;/strong&gt;&lt;br&gt;
Routing is a critical aspect of any web application, and Laravel provides a route caching mechanism for faster performance. To clear the route cache, use the following Artisan command:&lt;br&gt;
&lt;code&gt;php artisan route:cache&lt;/code&gt;&lt;br&gt;
This command will generate a new cached file for your routes, improving the efficiency of route registration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. View Cache:&lt;/strong&gt;&lt;br&gt;
If you are using view caching for faster rendering of views, you can clear the view cache with the following command:&lt;br&gt;
&lt;code&gt;php artisan view:clear&lt;/code&gt;&lt;br&gt;
This command will delete all compiled view files, allowing Laravel to recompile them as needed.&lt;br&gt;
&lt;strong&gt;4. Compiled Classes:&lt;/strong&gt;&lt;br&gt;
Laravel compiles various classes and services for optimization. To clear these compiled classes, use the clear-compiled Artisan command:&lt;br&gt;
&lt;code&gt;php artisan clear-compiled&lt;/code&gt;&lt;br&gt;
This command removes the compiled classes and services file, ensuring a clean slate for your application.&lt;br&gt;
&lt;strong&gt;5. Clear All Caches:&lt;/strong&gt;&lt;br&gt;
For a comprehensive cache clearance, you can use the cache:clear Artisan command:&lt;br&gt;
&lt;code&gt;php artisan cache:clear&lt;/code&gt;&lt;br&gt;
This command clears the entire cache, including the configuration, routes, and any other cached data.&lt;br&gt;
&lt;strong&gt;6. Clear Configuration and Route Cache in One Command:&lt;/strong&gt;&lt;br&gt;
To clear both the configuration and route cache in a single command, use:&lt;br&gt;
&lt;code&gt;php artisan optimize&lt;/code&gt;&lt;br&gt;
This command not only clears the configuration cache but also generates a new optimized class loader.&lt;/p&gt;

&lt;p&gt;In scenarios where SSH access is not available on shared hosting servers, clearing the Laravel cache requires an alternative approach. In such cases, we can utilize the routes defined in our application's routes/web.php file to trigger the necessary Laravel clear cache commands. This enables the clearance of Laravel cache by accessing specific routes through a web browser. This method offers a practical solution for shared hosting environments where direct server access via SSH is restricted.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//--- Clear route cache&lt;br&gt;
 Route::get('/route-cache', function() {&lt;br&gt;
     \Artisan::call('route:cache');&lt;br&gt;
     return 'Routes cache cleared';&lt;br&gt;
 });&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//--- Clear config cache&lt;br&gt;
 Route::get('/config-cache', function() {&lt;br&gt;
     \Artisan::call('config:cache');&lt;br&gt;
     return 'Config cache cleared';&lt;br&gt;
 });&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//---  Clear application cache&lt;br&gt;
 Route::get('/clear-cache', function() {&lt;br&gt;
     \Artisan::call('cache:clear');&lt;br&gt;
     return 'Application cache cleared';&lt;br&gt;
 });&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;//--- Clear view cache&lt;br&gt;
 Route::get('/view-clear', function() {&lt;br&gt;
     \Artisan::call('view:clear');&lt;br&gt;
     return 'View cache cleared';&lt;br&gt;
 });&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;code&gt;//--- Clear cache using reoptimized class&lt;br&gt;
 Route::get('/optimize-clear', function() {&lt;br&gt;
     \Artisan::call('optimize:clear');&lt;br&gt;
     return 'View cache cleared';&lt;br&gt;
 });&lt;/code&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
