<?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: Karan Datwani</title>
    <description>The latest articles on DEV Community by Karan Datwani (@karandatwani92).</description>
    <link>https://dev.to/karandatwani92</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%2F1411529%2F9519a947-02f2-4277-a97c-d2ee49dfeeb1.jpeg</url>
      <title>DEV Community: Karan Datwani</title>
      <link>https://dev.to/karandatwani92</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karandatwani92"/>
    <language>en</language>
    <item>
      <title>4 Ways To Prevent Race Conditions in Laravel</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Mon, 07 Jul 2025 11:48:00 +0000</pubDate>
      <link>https://dev.to/karandatwani92/4-ways-to-prevent-race-conditions-in-laravel-543j</link>
      <guid>https://dev.to/karandatwani92/4-ways-to-prevent-race-conditions-in-laravel-543j</guid>
      <description>&lt;p&gt;Imagine two users trying to update the same record at the exact same time. If your app isn’t handling this properly, one update might overwrite the other, leading to incorrect results. This issue is called a &lt;strong&gt;race condition&lt;/strong&gt;, and it can cause &lt;strong&gt;unexpected bugs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Consider an online payment system where a user’s account balance is updated. If two transactions occur at nearly the same time, they might read the same balance before either updates it—resulting in incorrect deductions.&lt;/p&gt;

&lt;p&gt;Is your app facing race conditions? It’s time to fix that. In this article, we’ll look at the &lt;strong&gt;benefits&lt;/strong&gt; and &lt;strong&gt;four ways to prevent race conditions&lt;/strong&gt;. This will help &lt;strong&gt;keep data safe and performance smooth&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Preventing Race Conditions
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Keeps Data Accurate&lt;/strong&gt; – Prevents critical information from being overwritten or corrupted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Avoids Lost Updates&lt;/strong&gt; – Makes sure every change is saved, even during high traffic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improves Stability&lt;/strong&gt; – Reduces strange bugs and makes your app more reliable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Increases Safety&lt;/strong&gt; – Blocks conflicting or unauthorized updates in real time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handles High Concurrency Smoothly&lt;/strong&gt; – Prevents bottlenecks and errors when many users interact with the same data at once.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Strategies to Prevent Race Conditions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Database Transactions
&lt;/h3&gt;

&lt;p&gt;Ensure &lt;strong&gt;multiple operations&lt;/strong&gt; succeed or fail together. If anything breaks, the transaction rolls back, preserving data consistency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A bank transfer where money is deducted from one account but fails to reach the other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$sender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nv"&gt;$receiver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nv"&gt;$sender&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$receiver&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nv"&gt;$sender&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$receiver&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ If any step fails, Laravel rolls back all changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Pessimistic Locking
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Locks a record&lt;/strong&gt; for the current operation, blocking others until it finishes. Great for &lt;strong&gt;high-traffic scenarios&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two customers try to buy the last item in stock.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lockForUpdate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Out of stock!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Ensures only one transaction can update the stock at a time.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Optimistic Locking
&lt;/h3&gt;

&lt;p&gt;Checks if a record changed before saving updates. Useful when many users may modify the same data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Two employees update a product’s stock simultaneously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$product&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;updated_at&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'updated_at'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;added_stock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nv"&gt;$product&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Stock data changed. Please refresh and try again."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Prevents updates if the data has changed since it was read.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Atomic Operations
&lt;/h3&gt;

&lt;p&gt;Let the database handle updates in a &lt;strong&gt;single step&lt;/strong&gt;—no fetch or manual save needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Multiple users try to withdraw money from the same account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'balance'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ The operation is handled directly in the database—no race condition.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus Tip: Cache Locks
&lt;/h3&gt;

&lt;p&gt;Use Laravel’s &lt;code&gt;Cache::lock()&lt;/code&gt; to prevent multiple processes from running the same critical logic block.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prevent multiple jobs from processing the same order at the same time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'process-order-123'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Lock for 10 seconds&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$lock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Execute payment or fulfillment logic&lt;/span&gt;
            &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'processing'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ChargeCustomer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$lock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;release&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Another process is handling it&lt;/span&gt;
    &lt;span class="nc"&gt;Log&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Order 123 is already being processed.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Prevents race conditions across processes by using cache-based locking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Word
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt; &lt;strong&gt;Race conditions can mess up your data&lt;/strong&gt; but Laravel gives you tools to stop them.&lt;/li&gt;
&lt;li&gt; Use &lt;strong&gt;database transactions&lt;/strong&gt; to guarantee all steps succeed together.&lt;/li&gt;
&lt;li&gt; Apply &lt;strong&gt;pessimistic locking&lt;/strong&gt; to block conflicts in real-time.&lt;/li&gt;
&lt;li&gt; Use &lt;strong&gt;optimistic locking&lt;/strong&gt; to catch conflicts before saving.&lt;/li&gt;
&lt;li&gt; Rely on &lt;strong&gt;atomic operations&lt;/strong&gt; to make safe one-step updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Don't leave data consistency to chance —build with confidence!&lt;/strong&gt; 🚀&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Share your thoughts or tips below.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;🚀 Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>🍣 Sushi — Your Eloquent model driver for other data sources</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Mon, 11 Nov 2024 06:54:46 +0000</pubDate>
      <link>https://dev.to/karandatwani92/sushi-your-eloquent-model-driver-for-other-data-sources-3db</link>
      <guid>https://dev.to/karandatwani92/sushi-your-eloquent-model-driver-for-other-data-sources-3db</guid>
      <description>&lt;p&gt;In Laravel projects, we usually store data in databases, create tables, and run migrations. But not every bit of data needs that level of complexity. Sometimes, you have small, static datasets—like a list of countries, settings, or configs—that hardly ever change. Setting up a database table for everything can feel like overkill.&lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;Sushi&lt;/strong&gt; comes in. It’s a lightweight package that lets you skip the database entirely for small, static data, even pulling from APIs or config arrays.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Laravel Sushi?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdh1vdc3i480xf31zvptv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdh1vdc3i480xf31zvptv.png" alt="image" width="800" height="599"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sushi allows you to create &lt;strong&gt;Eloquent models from data sources other than a database&lt;/strong&gt;. Instead of setting up a traditional database, you can provide data from an array, config, API, or a CSV file. What I mean is... it can be anything.&lt;/p&gt;

&lt;p&gt;It is created by &lt;a href="https://github.com/calebporzio" rel="noopener noreferrer"&gt;Caleb Porzio&lt;/a&gt; (the guy behind &lt;a href="https://livewire.laravel.com/" rel="noopener noreferrer"&gt;Livewire&lt;/a&gt; and &lt;a href="https://alpinejs.dev/" rel="noopener noreferrer"&gt;AlpineJS&lt;/a&gt;), Sushi simplifies your data when a full database table is unnecessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use Sushi?
&lt;/h2&gt;

&lt;p&gt;The simplest way to use &lt;a href="https://usesushi.dev" rel="noopener noreferrer"&gt;Sushi&lt;/a&gt; is by providing data as a &lt;strong&gt;hardcoded array&lt;/strong&gt;. But you can also pull dynamic data from an API or load it from a &lt;strong&gt;config array&lt;/strong&gt; for flexibility. Let’s explore three examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt; &lt;br&gt;
Let's do the installation first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require calebporzio/sushi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
`&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Hardcoded Data
&lt;/h3&gt;

&lt;p&gt;Here’s a basic model using a hardcoded array:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`php&lt;br&gt;
&amp;lt;?php&lt;/p&gt;

&lt;p&gt;namespace App\Models;&lt;/p&gt;

&lt;p&gt;use Sushi\Sushi;&lt;/p&gt;

&lt;p&gt;class Country extends Model&lt;br&gt;
{&lt;br&gt;
    // Add the Sushi trait&lt;br&gt;
    use Sushi;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Provide data as a hardcoded array
protected $rows = [
    ['id' =&amp;gt; 1, 'abbr' =&amp;gt; 'IN', 'label' =&amp;gt; 'India'],        
    ['id' =&amp;gt; 2, 'abbr' =&amp;gt; 'US', 'label' =&amp;gt; 'United States'],
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, you can query it just like a regular database model:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;php&lt;br&gt;
$india = Country::where('abbr', 'IN')-&amp;gt;first();&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Example 2: Data from a Config Array
&lt;/h3&gt;

&lt;p&gt;You can also load the data from a &lt;strong&gt;config file&lt;/strong&gt;. If you have data in some third-party config or you choose to keep the data outside the model, you can set up eloquent models from that data without going deeper into the code.&lt;/p&gt;

&lt;p&gt;Let’s say you have a config file like this in &lt;code&gt;config/countries.php&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`php&lt;br&gt;
&amp;lt;?php&lt;/p&gt;

&lt;p&gt;return [&lt;br&gt;
    ['id' =&amp;gt; 1, 'abbr' =&amp;gt; 'IN', 'label' =&amp;gt; 'India'],&lt;br&gt;
    ['id' =&amp;gt; 2, 'abbr' =&amp;gt; 'US', 'label' =&amp;gt; 'United States'],&lt;br&gt;
];&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, you can use Sushi to load this config data into a model:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`php&lt;br&gt;
&amp;lt;?php&lt;/p&gt;

&lt;p&gt;namespace App\Models;&lt;/p&gt;

&lt;p&gt;use Sushi\Sushi;&lt;/p&gt;

&lt;p&gt;class Country extends Model&lt;br&gt;
{&lt;br&gt;
    use Sushi;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function getRows()
{
    // Load the data from the config file
    return config('countries');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Dynamic Data from an API with Caching
&lt;/h3&gt;

&lt;p&gt;You can also implement Sushi where you pull data from an API. Here’s how you can fetch dynamic data from an API and cache it for an hour:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`php&lt;br&gt;
&amp;lt;?php&lt;/p&gt;

&lt;p&gt;namespace App\Models;&lt;/p&gt;

&lt;p&gt;use Sushi\Sushi;&lt;br&gt;
use Illuminate\Support\Facades\Http;&lt;br&gt;
use Illuminate\Support\Facades\Cache;&lt;/p&gt;

&lt;p&gt;class Country extends Model&lt;br&gt;
{&lt;br&gt;
    use Sushi;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function getRows()
{
    // Cache the API data for 1 hour
    return Cache::remember('countries', 60, function () {
        $response = Http::get('https://restcountries.com/v3.1/all');
        $countries = $response-&amp;gt;json();

        // Map the API response to the format Sushi expects
        return collect($countries)-&amp;gt;map(function ($country) {
            return [
                'id'    =&amp;gt; $country['cca3'],
                'abbr'  =&amp;gt; $country['cca2'],
                'label' =&amp;gt; $country['name']['common'],
            ];
        })-&amp;gt;toArray();
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;br&gt;
`&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We’re fetching data from the &lt;a href="https://restcountries.com/" rel="noopener noreferrer"&gt;REST Countries API&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The API response is cached for &lt;strong&gt;1 hour&lt;/strong&gt; using Laravel’s &lt;code&gt;Cache::remember()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;getRows()&lt;/code&gt; pulls and transforms the data into the format Sushi requires.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why Use Sushi?
&lt;/h2&gt;

&lt;p&gt;Sushi is perfect when you need a lightweight solution for static or semi-static datasets, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configurations, lists of countries, or languages&lt;/li&gt;
&lt;li&gt;Predefined roles or permissions&lt;/li&gt;
&lt;li&gt;Any small dataset that doesn’t change often but still needs to be queried&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can skip the database entirely and enjoy the full power of Eloquent without the overhead of migrations and database setup.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One more cool thing:&lt;/strong&gt; You don’t even need an &lt;code&gt;id&lt;/code&gt; column unless you want to. Sushi can auto-generate IDs for each row. But keep in mind, if the data changes and the cache is reset, these IDs might change too. So, for stability, it’s best to define your own &lt;code&gt;id&lt;/code&gt; column for each item.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Since Sushi uses SQLite to store the data, make sure the SQLite extension is enabled in your PHP configuration.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/calebporzio/sushi" rel="noopener noreferrer"&gt;Sushi&lt;/a&gt; lets you skip the database setup for smaller datasets and helps you define that data directly in the model, saving time and keeping things lightweight. It is a great tool when dealing with static data, configurations, or even dynamic data from APIs.&lt;/p&gt;

&lt;p&gt;Just a side note, our core product &lt;a href="https://github.com/laravel-backpack/crud" rel="noopener noreferrer"&gt;CRUD&lt;/a&gt; is also compatible with Sushi and you can use it to create a Table for the entries. Our &lt;a href="https://backpackforlaravel.com/products/devtools" rel="noopener noreferrer"&gt;Backpack DevTools&lt;/a&gt; also uses Sushi, which shows how practical this tool is. Give it a try - save time and keep things light.🧑‍💻😎&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>tutorial</category>
      <category>productivity</category>
      <category>php</category>
    </item>
    <item>
      <title>Backpack - Configure User Access Control and Permissions in 10 minutes</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Sun, 13 Oct 2024 10:17:29 +0000</pubDate>
      <link>https://dev.to/karandatwani92/backpack-configure-user-access-control-and-permissions-in-10-minutes-1pmk</link>
      <guid>https://dev.to/karandatwani92/backpack-configure-user-access-control-and-permissions-in-10-minutes-1pmk</guid>
      <description>&lt;p&gt;Hey folks! So, picture this: you're crafting your admin panel and the need for proper access control hits you.&lt;/p&gt;

&lt;p&gt;Admin panels without roles and permissions are like a party without bouncers – chaos waiting to happen. It's like giving everyone backstage access and waiting for disaster. We should return HTTP status code 403 to such unauthorized access events:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fer0k33ttdxynwc4qo5u4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fer0k33ttdxynwc4qo5u4.png" alt="image" width="714" height="371"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, we'll learn to keep things organized, secure and ensure the right people have the right keys using &lt;em&gt;Roles &amp;amp; permissions&lt;/em&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  PermissionManager
&lt;/h2&gt;

&lt;p&gt;To kick things off, I've equipped the &lt;a href="https://backpackforlaravel.com/" rel="noopener noreferrer"&gt;Backpack&lt;/a&gt; project with the &lt;a href="https://github.com/Laravel-Backpack/PermissionManager" rel="noopener noreferrer"&gt;PermissionManager&lt;/a&gt; Addon.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install &lt;code&gt;composer require backpack/permissionmanager&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Follow a few &lt;a href="https://github.com/Laravel-Backpack/PermissionManager?tab=readme-ov-file#install" rel="noopener noreferrer"&gt;steps&lt;/a&gt; and prepare your user model;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Boom! Your user model is now flexing some access control muscles:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffw9ykkpbpde4sb0y1o7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffw9ykkpbpde4sb0y1o7z.png" alt="image" width="800" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Claiming Territory with Role &amp;amp; Permissions
&lt;/h2&gt;

&lt;p&gt;When crafting roles and permissions, start by identifying distinct user responsibilities. Define them using Roles &amp;amp; Permissions CRUD:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Permissions grant users access to specific actions or URLs;&lt;/li&gt;
&lt;li&gt;Roles are your squad leaders, grouping permissions together;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For instance, distinguish between &lt;code&gt;Admins&lt;/code&gt; and &lt;code&gt;Editors&lt;/code&gt;. Create permissions for each role(examples: &lt;code&gt;manage articles&lt;/code&gt;, &lt;code&gt;manage tags&lt;/code&gt;). Admin role holders can swagger through CRUD operations while &lt;code&gt;Editors&lt;/code&gt; only have access to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Article&lt;/code&gt; CRUD and can perform actions like &lt;code&gt;delete&lt;/code&gt; and &lt;code&gt;edit&lt;/code&gt; on their posts only;&lt;/li&gt;
&lt;li&gt; No access to &lt;code&gt;Tags&lt;/code&gt; OR &lt;code&gt;Category&lt;/code&gt; CRUD;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Remember, clarity is key!&lt;/em&gt;&lt;/strong&gt; - Be granular and avoid unnecessary complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Securing the Resources
&lt;/h2&gt;

&lt;p&gt;To secure our admin panel, we need a bouncer at the gate. In our case, routes are the gates to our application and &lt;code&gt;backpack_middleware()&lt;/code&gt; is the bouncer. In &lt;code&gt;routes/backpack/custom.php&lt;/code&gt;, fortify your routes with backpack middleware:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::group([
    'prefix'     =&amp;gt; config('backpack.base.route_prefix', 'admin'),
    'middleware' =&amp;gt; ['web', backpack_middleware()],
    'namespace'  =&amp;gt; 'App\Http\Controllers\Admin',
], function () {
    // admin routes    
    Route::crud('article', 'ArticleCrudController');
    Route::crud('category', 'CategoryCrudController');
    Route::crud('tag', 'TagCrudController');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have placed a bouncer and he needs to know the rules to allow &amp;amp; deny user access, right?&lt;/p&gt;

&lt;h3&gt;
  
  
  Protecting Operations:
&lt;/h3&gt;

&lt;p&gt;Each backpack operation comes with a simple access system. It works using an array &lt;code&gt;$crud-&amp;gt;settings['operation_name']['access']&lt;/code&gt; which can either be &lt;em&gt;true or false&lt;/em&gt;. All operations by default run &lt;code&gt;CRUD::allowAccess('operation_name');&lt;/code&gt; which toggle that variable to true.&lt;/p&gt;

&lt;p&gt;You can add or remove elements to this access array in your setup() method using &lt;code&gt;CRUD::denyAccess()&lt;/code&gt; &amp;amp; &lt;code&gt;CRUD::allowAccess()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The following code is good enough to check the current user's permission (&lt;code&gt;manage articles&lt;/code&gt;) and restrict access to its &lt;code&gt;list&lt;/code&gt;, &lt;code&gt;show&lt;/code&gt;, &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;update&lt;/code&gt; and &lt;code&gt;delete&lt;/code&gt; operations of Article CRUD:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="no"&gt;CRUD&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ArticleCrudController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;CrudController&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
        &lt;span class="c1"&gt;// check permissions first&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;backpack_user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;can&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'manage articles'&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
        &lt;span class="c1"&gt;// deny access to operations&lt;/span&gt;
            &lt;span class="no"&gt;CRUD&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;denyAccess&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'list'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'show'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'create'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'update'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'delete'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="cd"&gt;/**
        ... rest of the code...
        **/&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To restrict per entry basis, I would add the following to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CRUD::operation(['list','update','delete'], function() {
    CRUD::setAccessCondition(['update', 'delete'], function ($entry) {
        return $entry-&amp;gt;author_id === backpack_user()-&amp;gt;id ? true : false;
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will remove &lt;code&gt;edit&lt;/code&gt; &amp;amp; &lt;code&gt;delete&lt;/code&gt; buttons and restrict the form URLs for the posts which are not created by the currently logged-in user:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx04w859l0qjl9fc7z5q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnx04w859l0qjl9fc7z5q.png" alt="image" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I find it cool, that small snippets take care of every single entry gate!&lt;/p&gt;

&lt;p&gt;We also need to &lt;strong&gt;hide items from the menu&lt;/strong&gt;. Admins should see only those menu items whose access they have. The following is a simple example of doing it using permissions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// resources/views/vendor/backpack/ui/inc/menu_items.blade.php&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;backpack_user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;can&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'manage articles'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;backpack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;menu&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Articles"&lt;/span&gt; &lt;span class="n"&gt;icon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"la la-newspaper-o"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"backpack_url('article')"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;endif&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;backpack_user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;can&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'manage categories'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;backpack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;menu&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Categories"&lt;/span&gt; &lt;span class="n"&gt;icon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"la la-list"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"backpack_url('category')"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;endif&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;backpack_user&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;can&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'manage tags'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;backpack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;menu&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Tags"&lt;/span&gt; &lt;span class="n"&gt;icon&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"la la-tag"&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"backpack_url('tag')"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="k"&gt;endif&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to make this work for Custom Operations?
&lt;/h3&gt;

&lt;p&gt;To set access control for your custom operation trait, use the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Think of a &lt;em&gt;key&lt;/em&gt; similar to your operation name or the action. Allow access to it while setting operation defaults via &lt;code&gt;CRUD::allowAccess('publish')&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Toggle visibility of UI elements such as button via &lt;code&gt;@if ($crud-&amp;gt;hasAccess('publish', $entry))&lt;/code&gt; in the blade file;&lt;/li&gt;
&lt;li&gt;Check access at the beginning of operation's function &lt;code&gt;CRUD::hasAccessOrFail('publish')&lt;/code&gt;;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;setupPublishDefaults&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;crud&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;allowAccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'publish'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Set operation defaults&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;CRUD&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;hasAccessOrFail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'publish'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Custom operation logic&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Protecting custom routes:
&lt;/h3&gt;

&lt;p&gt;Other than CRUD operations, you may have other admin panel routes to protect.&lt;/p&gt;

&lt;p&gt;Here, we would use Spatie's &lt;code&gt;permission&lt;/code&gt; middleware. We can use it like &lt;code&gt;can:list secrets&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;Route::group([    
&lt;/span&gt;    'prefix'     =&amp;gt; config('backpack.base.route_prefix', 'admin'),
&lt;span class="gi"&gt;+  'middleware' =&amp;gt; ['web','can:list secrets', backpack_middleware()],
&lt;/span&gt;], function () {
    Route::get('secrets', 'SecretController@list');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Huuhoo! We restricted access to a custom route too!&lt;/p&gt;

&lt;h3&gt;
  
  
  You may want to Spatie's directives like &lt;code&gt;@role&lt;/code&gt;, &lt;code&gt;@can&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Directive like &lt;code&gt;@can&lt;/code&gt;, &lt;code&gt;@role&lt;/code&gt; uses default laravel authentication guard. You can have two scenarios here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the default guard in &lt;code&gt;config.auth.defaults.guard&lt;/code&gt; to &lt;code&gt;backpack&lt;/code&gt;. In this case, make sure your roles and permissios are using the backpack guard in the database.&lt;/li&gt;
&lt;li&gt;Change the &lt;code&gt;config.backpack.base.guard&lt;/code&gt; to &lt;code&gt;null&lt;/code&gt; so backpack will also use the &lt;code&gt;web&lt;/code&gt; guard. In this case, make sure web is the guard in the database for permissions and roles.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Depending on your project needs, one of the configuration could be the best to apply.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There you have it! Your  admin panel is now a secure fortress. I hope you find this article an easy learn to implement access control on your admin panel.&lt;/p&gt;

&lt;p&gt;When you need a quick fix, consult the following cheat codes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// allow or deny access
CRUD::allowAccess(['list', 'create', 'delete']);
CRUD::denyAccess(['list', 'create', 'delete']);
CRUD::denyAllAccess();

// check access
CRUD::hasAccess('add'); // returns true/false
CRUD::hasAccess('add',$entry); // returns true/false
CRUD::hasAccessOrFail('add'); // throws 403 error
CRUD::hasAccessToAll(['create', 'update']); // returns true/false
CRUD::hasAccessToAny(['create', 'update']); // returns true/false

// allow or deny access depending on the entry
CRUD::operation(['list','update','delete'], function() {
        CRUD::setAccessCondition(['update', 'delete'], function ($entry) {
            return $entry-&amp;gt;id===1 ? true : false;
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more advanced features, head over to the &lt;a href="https://github.com/Laravel-Backpack/PermissionManager" rel="noopener noreferrer"&gt;PermissionManager&lt;/a&gt; or &lt;a href="https://spatie.be/docs/laravel-permission/v5/introduction" rel="noopener noreferrer"&gt;Spatie's package docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>security</category>
      <category>laravel</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Make Your WebApp or Admin Panel Installable as a Mobile App</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Mon, 30 Sep 2024 08:35:13 +0000</pubDate>
      <link>https://dev.to/karandatwani92/make-your-webapp-or-admin-panel-installable-as-a-mobile-app-3e6n</link>
      <guid>https://dev.to/karandatwani92/make-your-webapp-or-admin-panel-installable-as-a-mobile-app-3e6n</guid>
      <description>&lt;p&gt;Ever noticed a little icon or popup on your browser asking you to install the web app you're currently browsing? It’s a cool feature that lets users add a web app directly to their mobile home screen, just like a native app. Let's walk through how you can make your web app or admin panel installable!&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjo4do7tmapdbj3j2ytk.png" alt="image"&gt;&lt;/th&gt;
&lt;th&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F46pdahitxgrg6ed797an.png" alt="image"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What Is a Progressive Web App (PWA)?
&lt;/h2&gt;

&lt;p&gt;A quick primer: this feature is part of what’s called a Progressive Web App (&lt;a href="https://learn.microsoft.com/en-us/microsoft-edge/progressive-web-apps-chromium/" rel="noopener noreferrer"&gt;PWA&lt;/a&gt;). PWAs make your web app feel more like a native app, with the ability to be installed on a user’s device directly from their browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://open.spotify.com" rel="noopener noreferrer"&gt;Spotify&lt;/a&gt; has it, I've seen many e-commerce using it. I personally utilize it for my &lt;a href="https://www.bulkmake.com" rel="noopener noreferrer"&gt;e-commerce&lt;/a&gt; and some other &lt;a href="https://pro.samaksh.in" rel="noopener noreferrer"&gt;mini-web apps&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Make Your Web App Installable
&lt;/h2&gt;

&lt;p&gt;Here’s how you can make your web app installable as a mobile app with a simple manifest file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Create a Manifest File&lt;/strong&gt;: In your &lt;strong&gt;&lt;code&gt;public&lt;/code&gt;&lt;/strong&gt; directory, create a &lt;strong&gt;&lt;code&gt;site.webmanifest&lt;/code&gt;&lt;/strong&gt; with details like your app's name, start URL, and icons. Here’s a basic example:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"My Laravel Web App"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"short_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"WebApp"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start_url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"display"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"standalone"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"background_color"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"#ffffff"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"theme_color"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"#000000"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"icons"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/images/icons/icon-192x192.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"192x192"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"image/png"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"src"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"/images/icons/icon-512x512.png"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"sizes"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"512x512"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"image/png"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Link the Manifest&lt;/strong&gt;: Add this to the &lt;strong&gt;&lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt;&lt;/strong&gt; of your main Blade layout:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"manifest"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{{ asset('/site.webmanifest') }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it! Your Laravel web app is now ready to be installed on mobile devices directly from the browser. Visit your web app on a mobile or desktop browser like Chrome, or Safari. If everything is configured correctly, you should see an "Add to Home Screen" or "Install App" option added in the browser menu. You may also see a popup for the same if your browser supports it. Tap it and your web app will be installed just like a native mobile app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make Your Backpack AdminPanel Installable
&lt;/h2&gt;

&lt;p&gt;Some apps or sites may not make sense to offer this feature to the public, but offering the admin panel as a native mobile app would definitely be a good experience for admins.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://backpackforlaravel.com" rel="noopener noreferrer"&gt;Backpack&lt;/a&gt; offers you to do the above with just a &lt;a href="https://backpackforlaravel.com/docs/6.x/base-how-to#publish-mobile-and-favicon-headers-and-assets-1" rel="noopener noreferrer"&gt;command&lt;/a&gt;:&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 backpack:publish-header-metas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will ask you a few questions, and then it will publish the necessary files and add the headers. Later, you customize them to fit your branding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With a simple manifest file and a meta link in your Blade or HTML template, you can make your web app or admin panel installable on mobile devices. Try it out and make your web app more engaging!&lt;/p&gt;

&lt;p&gt;And… Suppose you or your admin want &lt;strong&gt;real-time mobile notifications&lt;/strong&gt; from your Laravel app. For example, when an order is received. In that case, You should check out our other article on &lt;a href="https://backpackforlaravel.com/articles/tutorials/receive-notifications-from-your-laravel-app-using-slack-with-a-10-minutes-setup" rel="noopener noreferrer"&gt;receiving Slack notifications from your Laravel app with a 10-minute setup&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>html</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Receive Slack Notifications from your Laravel App with a 10-minute Setup</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Sun, 22 Sep 2024 09:05:15 +0000</pubDate>
      <link>https://dev.to/karandatwani92/receive-slack-notifications-from-your-laravel-app-with-a-10-minute-setup-2be1</link>
      <guid>https://dev.to/karandatwani92/receive-slack-notifications-from-your-laravel-app-with-a-10-minute-setup-2be1</guid>
      <description>&lt;p&gt;In the previous &lt;a href="https://backpackforlaravel.com/articles/tutorials/new-in-v6-a-larger-layout-for-your-large-set-of-menu-items" rel="noopener noreferrer"&gt;article&lt;/a&gt;, I introduced a Backpack's new &lt;a href="https://backpackforlaravel.com/docs/6.x/theme-tabler#menu-dropdown-column-1" rel="noopener noreferrer"&gt;Menu Dropdown Column&lt;/a&gt; component which I use for my &lt;a href="https://www.bulkmake.com" rel="noopener noreferrer"&gt;e-commerce&lt;/a&gt; admin panel. Today, I will talk about a Laravel feature I use to receive Order Notifications. Whenever an order comes, my team is notified to ship it ASAP. You can assume the importance of receiving quick notifications for such events.&lt;/p&gt;

&lt;p&gt;I also use it in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://github.com/Laravel-Backpack/LogManager" rel="noopener noreferrer"&gt;Log Manager&lt;/a&gt;: To get notified of website error events ASAP.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/Laravel-Backpack/BackupManager" rel="noopener noreferrer"&gt;Backup Manager&lt;/a&gt;: To get notified if the backup fails.&lt;/li&gt;
&lt;li&gt;To receive notification if some Queued Job/Background running task fails.&lt;/li&gt;
&lt;li&gt;To receive Daily reports.&lt;/li&gt;
&lt;li&gt;And... Many events(Getting live updates fasten up the process)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Laravel officially supports it. Thus, many laravel packages supports it too.&lt;/p&gt;

&lt;p&gt;In this tutorial, I assume you have a basic knowledge of &lt;a href="https://laravel.com/docs/11.x/notifications" rel="noopener noreferrer"&gt;Laravel Notifications&lt;/a&gt; and you know about &lt;a href="https://slack.com" rel="noopener noreferrer"&gt;Slack&lt;/a&gt;. Slack is a messaging app designed for business use. It can be used on desktop and mobile devices, making communications easy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a Slack App
&lt;/h2&gt;

&lt;p&gt;First, we need a Slack account to create a new #Channel &amp;amp; our Slack App. The following steps will help you to create one for your Laravel app.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Steps&lt;/th&gt;
&lt;th&gt;Screenshot&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. &lt;a href="https://slack.com/signin" rel="noopener noreferrer"&gt;Sign in on Slack&lt;/a&gt; &amp;amp; Start with free plan.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq4webqhb7k6rz892oe38.png" alt="1" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2. Go to &lt;a href="https://api.slack.com/apps" rel="noopener noreferrer"&gt;https://api.slack.com/apps&lt;/a&gt;. Click on &lt;strong&gt;Create an app&lt;/strong&gt; &amp;amp; select &lt;strong&gt;From scratch&lt;/strong&gt; as we are building a simple app.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F35epmfd2524rvv68rm2a.png" alt="2" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3. Next, you will be asked to create or select a workspace. Just name it.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycebmy2so0ocp1qwf1sq.png" alt="3" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4. After workspace, You can &lt;strong&gt;Create an App&lt;/strong&gt; for that.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyset8co1wgmxbvkv58vv.png" alt="4" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5. &lt;strong&gt;Name&lt;/strong&gt; your Slack App &amp;amp; choose the workspace.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fft71y53oo9xjbrsu6746.png" alt="5" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6. Next, click on &lt;strong&gt;Incoming Webhooks&lt;/strong&gt;.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcchg4r6h22f8du5v107t.png" alt="6" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7. &lt;strong&gt;Activate!&lt;/strong&gt; Incoming Webhooks &amp;amp; Click "Add New Webhook to Workspace".&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faavlt26o6ceysdi69ra3.png" alt="7" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8. Choose a &lt;strong&gt;channel&lt;/strong&gt; where this webhook will send messages.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyds8piub2ttz6avuaqfn.png" alt="8" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9. Final Step! Navigate to &lt;strong&gt;OAuth &amp;amp; Permissions&lt;/strong&gt;. Add scopes such as &lt;code&gt;chat:write&lt;/code&gt;, &lt;code&gt;incoming-webhook&lt;/code&gt; ,&lt;code&gt;chat:write.public&lt;/code&gt; and copy the &lt;strong&gt;OAuth Token&lt;/strong&gt;.&lt;/td&gt;
&lt;td&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5wh7fnm7p9igbn06p0d.png" alt="9" width="" height=""&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Done! You need to paste the Token &amp;amp; Webhook URL into Your Laravel App to send notifications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Package Installation
&lt;/h2&gt;

&lt;p&gt;Install the official &lt;strong&gt;Slack Notification Channel&lt;/strong&gt; package:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require laravel/slack-notification-channel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Paste the Webhook &amp;amp; Oauth Token in your Laravel configuration.
&lt;/h3&gt;

&lt;p&gt;Use them within the slack configuration array in  &lt;code&gt;config/services.php&lt;/code&gt;. Your Laravel App is now ready to send notifications.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'slack' =&amp;gt; [
    'notifications' =&amp;gt; [
        'bot_user_oauth_token' =&amp;gt; env('SLACK_BOT_USER_OAUTH_TOKEN'),
        'channel' =&amp;gt; env('SLACK_BOT_USER_DEFAULT_CHANNEL'),
    ],
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Send Notifications
&lt;/h2&gt;

&lt;p&gt;In general, multiple channels are created depending on the type of notification. I created two more channels &amp;amp; two webhooks for each channel. For example: &lt;code&gt;#orders&lt;/code&gt;, &lt;code&gt;#errors-logs&lt;/code&gt;, &lt;code&gt;#backup-logs&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use webhook URL in the packages.
&lt;/h3&gt;

&lt;p&gt;Each package that supports Slack notifications comes with a config file where we only need to configure the webhook URL.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For Backup Manager, paste the webhook URL in  &lt;code&gt;config/backup.php&lt;/code&gt; &amp;amp; enable the channel for package's notification class:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'notifications' =&amp;gt; [
-    \Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class =&amp;gt; ['mail'],
+    \Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class =&amp;gt; ['slack'],
    ...            
    ],

'slack' =&amp;gt; [
+   'webhook_url' =&amp;gt; env('SLACK_BOT_BACKUP_WEBHOOK_URL'),           
    ...
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;For Log Manager, paste the webhook URL in  &lt;code&gt;config/logging.php&lt;/code&gt; to enable it:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'slack' =&amp;gt; [
+   'url' =&amp;gt; env('SLACK_BOT_ERROR_LOG_WEBHOOK_URL'),   
    ...
],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thats it! These packages will be sending the notifications on their configured events.🔔&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Custom Notification
&lt;/h3&gt;

&lt;p&gt;Now let's talk about creating Notification as per our needs.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Command to create a notification class:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:notification OrderNotification
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add a &lt;code&gt;toSlack()&lt;/code&gt; method and format the notification:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Notifications\Slack\SlackMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toSlack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$notifiable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SlackMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"#orders"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// set the channel&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Check &amp;amp; update order status.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;headerBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'#'&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="s1"&gt;' Order Received🎉'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;You can find a set of blocks &lt;a href="https://laravel.com/docs/11.x/notifications#formatting-slack-notifications" rel="noopener noreferrer"&gt;here&lt;/a&gt; for formatting  beautiful Slack notifications. You can also give action choices to the user with &lt;code&gt;actionsBlock()&lt;/code&gt; available &lt;a href="https://laravel.com/docs/11.x/notifications#slack-interactivity" rel="noopener noreferrer"&gt;here.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Enable &lt;code&gt;slack&lt;/code&gt; delivery channel for the notification:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&lt;span class="p"&gt;public function via($notifiable)
&lt;/span&gt;{
&lt;span class="gd"&gt;-    return ['mail'];
&lt;/span&gt;&lt;span class="gi"&gt;+    return ['mail', 'slack'];
&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Tip: You can turn channels on/off for each Notification by conditionally altering the array.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Done! Our Notification is ready to send on Slack.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For a better understanding of &lt;code&gt;Notificaton::class&lt;/code&gt;, You can check this &lt;a href="https://gist.github.com/karandatwani92/cbf83a72605f6994e9a9a0eed7ebb323" rel="noopener noreferrer"&gt;Gist&lt;/a&gt;.  I use it to send &lt;code&gt;E-mail&lt;/code&gt; &amp;amp; &lt;code&gt;SMS&lt;/code&gt; to user and receive &lt;code&gt;Slack&lt;/code&gt; notification when an order gets placed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  We can send the notification in two ways; &lt;a href="https://laravel.com/docs/11.x/notifications#sending-notifications" rel="noopener noreferrer"&gt;Using Trait&lt;/a&gt; and &lt;a href="https://laravel.com/docs/11.x/notifications#using-the-notification-facade" rel="noopener noreferrer"&gt;Facade&lt;/a&gt;:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Using &lt;code&gt;Notifiable&lt;/code&gt; trait, For example we can use this in User Model:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight diff"&gt;&lt;code&gt;&amp;lt;?php
&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;namespace App\Models;
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;use Illuminate\Foundation\Auth\User as Authenticatable;
&lt;/span&gt;&lt;span class="gi"&gt;+ use Illuminate\Notifications\Notifiable;
&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;span class="p"&gt;class User extends Authenticatable
&lt;/span&gt;{
&lt;span class="gi"&gt;+    use Notifiable;
&lt;/span&gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and then send (☝️ check Gist to see how i pass order information.):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OrderNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Another way is using Notification facade. This approach is useful when you need to send a notification to multiple notifiable entities such as a collection of users:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Notification&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Notification&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OrderNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nc"&gt;Notification&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'slack'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'SLACK_WEBHOOK'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleNotification&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Get more out of Slack Notifications
&lt;/h3&gt;

&lt;p&gt;You can find a set of blocks &lt;a href="https://laravel.com/docs/11.x/notifications#formatting-slack-notifications" rel="noopener noreferrer"&gt;here&lt;/a&gt; for formatting beautiful Slack Notifications. You can also give action choices to the user with &lt;code&gt;actionsBlock()&lt;/code&gt; available &lt;a href="https://laravel.com/docs/11.x/notifications#slack-interactivity" rel="noopener noreferrer"&gt;here.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We often need to get notified when something happens on the App. Get timely updates from your Laravel application with the help of Slack notifications. You can set it up in just 10 minutes!&lt;/p&gt;

&lt;p&gt;I hope this article was useful and saved you some time figuring out this yourself. Just don't get into Bombing your Slack channels😝!&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Add Engaging Animations To Your Webapp for FREE</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Mon, 09 Sep 2024 09:26:19 +0000</pubDate>
      <link>https://dev.to/karandatwani92/add-engaging-animations-to-your-webapp-for-free-52lc</link>
      <guid>https://dev.to/karandatwani92/add-engaging-animations-to-your-webapp-for-free-52lc</guid>
      <description>&lt;p&gt;Lottie animations have become a popular choice for adding rich, engaging animations to websites and apps. They’re lightweight, scalable, and easy to implement, making them a go-to solution for designers and developers alike. In this tutorial, we'll explore what Lottie animations are, how to use them, and where to get them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fairbnb%2Flottie-web%2Fmaster%2Fgifs%2FExample2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fairbnb%2Flottie-web%2Fmaster%2Fgifs%2FExample2.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is Lottie?
&lt;/h1&gt;

&lt;p&gt;Lottie is a library developed by &lt;a href="https://airbnb.io/lottie" rel="noopener noreferrer"&gt;Airbnb&lt;/a&gt; that renders animations in real-time on the web and mobile apps. It reads JSON files exported from &lt;strong&gt;Adobe After Effects&lt;/strong&gt; using the &lt;a href="https://lottiefiles.com/tutorials/how-to-export-using-bodymovin-to-lottie-from-after-effects-d1wDl5A9yQk" rel="noopener noreferrer"&gt;Bodymovin&lt;/a&gt; plugin, allowing complex animations to be easily integrated without compromising performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use Lottie?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lightweight&lt;/strong&gt;: Compared to traditional video files or GIFs, Lottie animations are extremely small in size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalable&lt;/strong&gt;: Because they’re vector-based, Lottie animations remain crisp and clear at any size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cross-Platform&lt;/strong&gt;: Lottie works seamlessly across different platforms, including web, iOS, and Android.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Easy to Integrate&lt;/strong&gt;: With the Lottie library, implementing animations is straightforward, requiring just a few lines of code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Community Resources&lt;/strong&gt;: Lottie has an active community, sharing many animations for FREE.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://lottiefiles.com/" rel="noopener noreferrer"&gt;LottieFiles&lt;/a&gt; is a go-to resource for Lottie animations. It provides a vast library of FREE and premium animations, tools for creating and editing animations, and even plugins for easy integration with many platforms like &lt;code&gt;vanilaJS&lt;/code&gt;, &lt;code&gt;React&lt;/code&gt;, &lt;code&gt;VueJS&lt;/code&gt;, &lt;code&gt;IOS&lt;/code&gt;, &lt;code&gt;Android&lt;/code&gt;, etc.&lt;/p&gt;

&lt;p&gt;Visit &lt;a href="https://lottiefiles.com/featured" rel="noopener noreferrer"&gt;LottieFiles.com&lt;/a&gt; and search through categories to find animations that suit your needs. &lt;strong&gt;Download&lt;/strong&gt; the animation you like in the JSON format.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Lottie Animations to Your Project
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Add an element where your animation will appear:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;canvas&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"canvas"&lt;/span&gt; &lt;span class="na"&gt;width=&lt;/span&gt;&lt;span class="s"&gt;"300"&lt;/span&gt; &lt;span class="na"&gt;height=&lt;/span&gt;&lt;span class="s"&gt;"300"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/canvas&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Include the Lottie &lt;a href="https://developers.lottiefiles.com/docs/dotlottie-player/dotlottie-web/" rel="noopener noreferrer"&gt;Web library&lt;/a&gt; and play the animation with JavaScript:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"module"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;DotLottie&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://cdn.jsdelivr.net/npm/@lottiefiles/dotlottie-web/+esm&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DotLottie&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;autoplay&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;canvas&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://lottie.host/4db68bbd-31f6-4cd8-84eb-189de081159a/IGmMCqhzpt.lottie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// or .json file&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Wanna Embed into HTML or Wordpress?
&lt;/h3&gt;

&lt;p&gt;That's also possible. Here is an example of embedding a lottie animation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;lottie-player&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://lottie.host/4c2a69ec-47c3-49ae-b296-8a4770ce5cf5/sl7OhHvAbk.json"&lt;/span&gt; &lt;span class="na"&gt;background=&lt;/span&gt;&lt;span class="s"&gt;"#FFFFFF"&lt;/span&gt; &lt;span class="na"&gt;speed=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width: 300px; height: 300px"&lt;/span&gt; &lt;span class="na"&gt;loop&lt;/span&gt; &lt;span class="na"&gt;controls&lt;/span&gt; &lt;span class="na"&gt;autoplay&lt;/span&gt; &lt;span class="na"&gt;direction=&lt;/span&gt;&lt;span class="s"&gt;"1"&lt;/span&gt; &lt;span class="na"&gt;mode=&lt;/span&gt;&lt;span class="s"&gt;"normal"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/lottie-player&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Where is IOS, Android, React or Vue.. integration?
&lt;/h3&gt;

&lt;p&gt;For other platforms, find the suitable lottie player &lt;a href="https://developers.lottiefiles.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Lottie animations offer a way to enhance the user experience with rich, high-quality animations. With resources like &lt;a href="https://lottiefiles.com/" rel="noopener noreferrer"&gt;LottieFiles.com&lt;/a&gt;, integrating and customizing these animations has never been easier. Whether you're a designer or a developer, adding Lottie animations to your toolkit can elevate your projects and make them stand out.🥇&lt;/p&gt;

&lt;p&gt;Thanks for reading. For more articles, you can subscribe to our &lt;a href="https://backpackforlaravel.com/newsletter" rel="noopener noreferrer"&gt;newsletter&lt;/a&gt;.💬&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>animation</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Top 10 PHP Features You Can Use in 2024</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Mon, 09 Sep 2024 09:05:20 +0000</pubDate>
      <link>https://dev.to/karandatwani92/top-10-php-features-you-can-use-in-2024-3eg7</link>
      <guid>https://dev.to/karandatwani92/top-10-php-features-you-can-use-in-2024-3eg7</guid>
      <description>&lt;p&gt;Hey PHP fans! This article highlights some excellent new features of our favorite scripting language. Whether you're a seasoned pro or just starting out, these will make your coding life easier and more fun. Let's dive into the top PHP features you can use right now!&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Readonly Properties&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let's say you don't want changes to a variable after initialization. Now, with &lt;a href="https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties" rel="noopener noreferrer"&gt;readonly&lt;/a&gt; properties, you can set a property once and prevent it from being modified.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;strong&gt;Enums&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.php.net/manual/en/language.types.enumerations.php#language.types.enumerations.basics" rel="noopener noreferrer"&gt;Enums&lt;/a&gt; are now a thing in PHP! They let you define a set of named values, perfect for things like statuses or categories.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="n"&gt;enum&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;ACTIVE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="no"&gt;INACTIVE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Status&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ACTIVE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;strong&gt;Match Expressions&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://www.php.net/manual/en/control-structures.match.php#control-structures.match" rel="noopener noreferrer"&gt;Match&lt;/a&gt; expression is a more flexible alternative to switch statements. They let you return values directly from each case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'active'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s1"&gt;'active'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'The user is active.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'inactive'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'The user is inactive.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'pending'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'The user is pending.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Unknown status.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. &lt;strong&gt;Constructor Property Promotion&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Save time by defining and initializing properties directly in the &lt;a href="https://wiki.php.net/rfc/constructor_promotion" rel="noopener noreferrer"&gt;constructor&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nv"&gt;$x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="nv"&gt;$y&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nv"&gt;$point&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Point&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;2.5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. &lt;strong&gt;Named Arguments&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://wiki.php.net/rfc/named_params" rel="noopener noreferrer"&gt;Named arguments&lt;/a&gt; make your code more readable by allowing you to pass values to a function by name instead of position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nv"&gt;$isAdmin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Your code here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;createUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'john_doe'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isAdmin&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. &lt;strong&gt;Nullsafe Operator&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Avoid those annoying null checks with the &lt;a href="https://wiki.php.net/rfc/nullsafe_operator" rel="noopener noreferrer"&gt;nullsafe operator&lt;/a&gt;, which lets you call methods or access properties on an object only if it's not null.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;?-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getProfile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;?-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getBio&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. &lt;strong&gt;Union Types&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Type hinting just got more flexible with &lt;a href="https://www.php.net/manual/en/language.types.type-system.php#language.types.type-system.composite.union" rel="noopener noreferrer"&gt;union&lt;/a&gt; types, allowing you to specify multiple types for a parameter or return value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;processNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="nv"&gt;$number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Array Unpacking with String Keys
&lt;/h3&gt;

&lt;p&gt;Array unpacking with string keys, making it easier to merge arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$array1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nv"&gt;$array2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'c'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="nv"&gt;$array1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nb"&gt;print_r&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$array2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Output: ['c' =&amp;gt; 3, 'a' =&amp;gt; 1, 'b' =&amp;gt; 2]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9. JSON_THROW_ON_ERROR
&lt;/h3&gt;

&lt;p&gt;With PHP 8.3, you can enable json.exceptions to throw &lt;a href="https://www.php.net/manual/en/class.jsonexception.php" rel="noopener noreferrer"&gt;JsonException&lt;/a&gt; by default on JSON errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nb"&gt;ini_set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'json.exceptions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'1'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;json_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'{"invalidJson":}'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;JsonException&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="s1"&gt;'JSON Error: '&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10. &lt;strong&gt;JIT Compilation&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Just-In-Time (JIT) compilation is now part of PHP, making your scripts run faster by compiling parts of the code at runtime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;2024 is shaping up to be an exciting year for PHP developers. With these new features, you'll write cleaner, faster, and more readable code. So update your PHP version and start playing with these cool new features.&lt;/p&gt;

&lt;p&gt;All the above have been previously shared on our Twitter, one by one. &lt;a href="https://twitter.com/laravelbackpack" rel="noopener noreferrer"&gt;Follow us on Twitter&lt;/a&gt;; You'll ❤️ it. You can also check our FREE &lt;a href="https://backpackforlaravel.com/articles/tips-and-tricks/laravel-advanced-top-5-scheduler-functions-you-might-not-know-about" rel="noopener noreferrer"&gt;Laravel Advanced series&lt;/a&gt; to know trending Laravel features. Keep exploring, and keep coding. Until next time, happy coding! 🚀&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Laravel Advanced: Top 10 Validation Rules You Didn't Know Existed</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Fri, 12 Jul 2024 08:42:52 +0000</pubDate>
      <link>https://dev.to/karandatwani92/laravel-advanced-top-10-validation-rules-you-didnt-know-existed-3mk1</link>
      <guid>https://dev.to/karandatwani92/laravel-advanced-top-10-validation-rules-you-didnt-know-existed-3mk1</guid>
      <description>&lt;p&gt;Do you know all the validation rules available in Laravel? Think again! Laravel has many ready-to-use validation rules that can make your code life a whole lot easier. Let’s uncover the top 10 validation rules you probably didn’t know existed.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Prohibited&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Want to make sure a field is not present in the input? Use &lt;code&gt;prohibited&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'prohibited'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;username&lt;/code&gt; is included in the request, validation will fail. Simple and effective, especially for a honeypot!&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Prohibits&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Need a field to prohibit another field from being present? Check this out.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'prohibits:username'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;password&lt;/code&gt; is present, &lt;code&gt;username&lt;/code&gt; must not be.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Required If&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This one’s a lifesaver when you need conditional validation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required_if:contact_method,email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;email&lt;/code&gt; field is required only if &lt;code&gt;contact_method&lt;/code&gt; is &lt;code&gt;email&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Required Unless&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Opposite of &lt;code&gt;required_if&lt;/code&gt;. Use it to require a field unless another field has a specific value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required_unless:contact_method,phone'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;email&lt;/code&gt; is required unless &lt;code&gt;contact_method&lt;/code&gt; is &lt;code&gt;phone&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Required Without&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This rule is great when you need a field only if another field isn’t present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required_without:phone'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;phone&lt;/code&gt; isn’t provided, &lt;code&gt;email&lt;/code&gt; must be.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Required Without All&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Step up your game by requiring a field if none of the other specified fields are present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'email'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'required_without_all:phone,address'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If neither &lt;code&gt;phone&lt;/code&gt; nor &lt;code&gt;address&lt;/code&gt; is present, &lt;code&gt;email&lt;/code&gt; is required.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Starts With&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Check if a string starts with a given value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'starts_with:admin,user'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;username&lt;/code&gt; must start with either &lt;code&gt;admin&lt;/code&gt; or &lt;code&gt;user&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. &lt;strong&gt;Ends With&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Similarly, check if a string ends with a specific value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'username'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'ends_with:_admin,_user'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;username&lt;/code&gt; must end with either &lt;code&gt;_admin&lt;/code&gt; or &lt;code&gt;_user&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. &lt;strong&gt;In Array&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Confirm a field’s value exists in another &lt;strong&gt;array field&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'selected_option'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'in_array:available_options.*'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;selected_option&lt;/code&gt; must be one of the values in the &lt;code&gt;available_options&lt;/code&gt; array.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. &lt;strong&gt;Different&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Make sure two fields have different values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'new_password'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'different:current_password'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;new_password&lt;/code&gt; must be different from the &lt;code&gt;current_password&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;p&gt;So there you have it, folks! Ten super handy Laravel validation rules you might not have known about. Using these can save you time and make your code cleaner and more efficient.&lt;/p&gt;

&lt;p&gt;All the above have been previously shared on our Twitter, one by one. &lt;a href="https://twitter.com/laravelbackpack" rel="noopener noreferrer"&gt;Follow us on Twitter&lt;/a&gt;; You'll ❤️ it.&lt;/p&gt;

&lt;p&gt;You can also check the first article of the series, which is on the &lt;a href="https://backpackforlaravel.com/articles/tips-and-tricks/laravel-advanced-top-5-scheduler-functions-you-might-not-know-about" rel="noopener noreferrer"&gt;Top 5 Scheduler Functions you might not know about&lt;/a&gt;. Keep exploring, and keep coding with ease using Laravel. Until next time, happy coding! 🚀&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>validation</category>
    </item>
    <item>
      <title>Laravel Caching - Explained Simply</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Sat, 06 Jul 2024 17:47:54 +0000</pubDate>
      <link>https://dev.to/karandatwani92/laravel-caching-explained-simply-2ef0</link>
      <guid>https://dev.to/karandatwani92/laravel-caching-explained-simply-2ef0</guid>
      <description>&lt;p&gt;Caching is like keeping your favorite toy right on top of your toy box, so you can grab it quickly whenever you want to play.&lt;/p&gt;

&lt;p&gt;Similarly, cache in Laravel stores data so your website can show it quickly without searching or querying all over again. Just like finding your toy faster, caching helps websites load quickly.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does Caching Work in Laravel?
&lt;/h2&gt;

&lt;p&gt;Laravel has a built-in storage called &lt;code&gt;cache&lt;/code&gt;. It helps you store data and quickly get it later. &lt;/p&gt;

&lt;h3&gt;
  
  
  Storing Data in the Cache
&lt;/h3&gt;

&lt;p&gt;For example - weather data. Weather won't change on every request, so why make a DB or API call every time? It would be way more efficient to keep the info handy in the cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$weatherData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getWeatherFromService&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'current_weather'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$weatherData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;current_weather&lt;/code&gt; is the cache key, &lt;code&gt;$weatherData&lt;/code&gt; is the info, and &lt;code&gt;60&lt;/code&gt; is the minutes to keep it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retrieving and Checking Data
&lt;/h3&gt;

&lt;p&gt;To get weather data from the cache:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$weatherData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'current_weather'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To check if the data is still there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'current_weather'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// It's there!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Deleting Data from the Cache
&lt;/h3&gt;

&lt;p&gt;To refresh weather data, remove old info:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'current_weather'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Cool Things You Can Do with Caching
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;For a busy blog or for an online shop, cache posts &amp;amp; products to boost speed:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Cache&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$blogPosts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'blog_posts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'posts'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nv"&gt;$productList&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'product_list'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use Model events to setup cache automation, to keep cache data up to date. Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Models&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Model&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Facades\Cache&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nv"&gt;$fillable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'content'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;boot&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;boot&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;retrieved&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"post_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;created&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'all_posts'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"post_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;updated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"post_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'all_posts'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"post_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="k"&gt;static&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;deleted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"post_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;forget&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'all_posts'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getAllPosts&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'all_posts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;searchPosts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"search_posts_&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'title'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'like'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;%"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                       &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;orWhere&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'content'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'like'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"%&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;%"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                       &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Caching in Laravel makes websites faster by storing data for quick access. Start using caching to speed up your Laravel app and make users happy!&lt;/p&gt;

&lt;p&gt;All of the above have been previously shared on our Twitter, one by one. If you're on Twitter, &lt;a href="https://twitter.com/laravelbackpack" rel="noopener noreferrer"&gt;follow us&lt;/a&gt; - you'll ❤️ it. You can also check the first article of the series, which is on the &lt;a href="https://backpackforlaravel.com/articles/tips-and-tricks/laravel-advanced-top-5-scheduler-functions-you-might-not-know-about" rel="noopener noreferrer"&gt;Top 5 Scheduler Functions you might not know about&lt;/a&gt;. Keep exploring, and keep coding with ease using Laravel. Until next time, happy caching! 🚀&lt;/p&gt;

</description>
      <category>learning</category>
      <category>laravel</category>
      <category>development</category>
      <category>caching</category>
    </item>
    <item>
      <title>Laravel Advanced: Top 5 Scheduler Functions You Might Not Know About</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Mon, 17 Jun 2024 10:04:34 +0000</pubDate>
      <link>https://dev.to/karandatwani92/laravel-advanced-top-5-scheduler-functions-you-might-not-know-about-3mef</link>
      <guid>https://dev.to/karandatwani92/laravel-advanced-top-5-scheduler-functions-you-might-not-know-about-3mef</guid>
      <description>&lt;p&gt;In this article series, we go a little deeper into parts of Laravel we all use, to uncover functions and features that we can use in our next projects... if only we knew about them! Our first article in the series is about the &lt;a href="https://laravel.com/docs/11.x/scheduling"&gt;Laravel Scheduler&lt;/a&gt; - which helps run scheduled tasks (aka cron jobs).&lt;/p&gt;

&lt;p&gt;Let's explore a few lesser-known scheduler functions:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. skip() &amp;amp; when()
&lt;/h3&gt;

&lt;p&gt;If you want your scheduled task to execute only when some condition is &lt;code&gt;true&lt;/code&gt;, use &lt;code&gt;when()&lt;/code&gt; to set such conditions inline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$schedule&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'your:command'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;when&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;some_condition&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;skip()&lt;/code&gt; is the exact opposite of the &lt;code&gt;when()&lt;/code&gt; method. If the skip method returns &lt;code&gt;true&lt;/code&gt;, the scheduled task will not be executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$schedule&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'emails:send'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;daily&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;skip&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Calendar&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;isHolidauy&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. withoutOverlapping()
&lt;/h3&gt;

&lt;p&gt;You may be running a critical job that should only have one instance running at a time. That's where &lt;code&gt;withoutOverlapping()&lt;/code&gt; ensures that a scheduled task won't overlap, preventing potential conflicts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$schedule&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'your:command'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;withoutOverlapping&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. thenPing()
&lt;/h3&gt;

&lt;p&gt;After executing a task, you might want to ping a URL to notify another service or trigger another action. &lt;code&gt;thenPing()&lt;/code&gt; lets you do just that seamlessly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$schedule&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'your:command'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;thenPing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'http://example.com/webhook'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. runInBackground()
&lt;/h3&gt;

&lt;p&gt;If you want your scheduled task to run in the background without holding up other processes. &lt;code&gt;runInBackground()&lt;/code&gt; will help you do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$schedule&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'your:command'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;runInBackground&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. evenInMaintenanceMode()
&lt;/h3&gt;

&lt;p&gt;You can guess what it does by its name. You can execute scheduled tasks even when your application is in maintenance mode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$schedule&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'your:command'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;evenInMaintenanceMode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;That's all for now, folks! Give them a try; use these scheduler functions in your task automation and make your code much easier. All of the above have been previously shared on our Twitter, one by one. &lt;a href="https://twitter.com/laravelbackpack"&gt;Follow us on Twitter&lt;/a&gt;; You'll ❤️ it.&lt;/p&gt;

&lt;p&gt;Keep exploring, keep coding, and keep pushing the boundaries of what you can achieve with Laravel. Until next time, happy scheduling! 🚀&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>cronjob</category>
      <category>automation</category>
    </item>
    <item>
      <title>Top 10 Laravel Collection Methods You Have Never Used.</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Sun, 09 Jun 2024 10:03:08 +0000</pubDate>
      <link>https://dev.to/karandatwani92/top-10-laravel-collection-methods-you-have-never-used-62h</link>
      <guid>https://dev.to/karandatwani92/top-10-laravel-collection-methods-you-have-never-used-62h</guid>
      <description>&lt;p&gt;In this article series, we go a little deeper into parts of Laravel we all use, to uncover functions and features that we can use in our next projects... if only we knew about them!&lt;/p&gt;

&lt;p&gt;Here are a few lesser-known collection methods that can be quite handy in various real-world scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;macro()&lt;/strong&gt;: This lets you add custom methods to Laravel collections that can be used on any collection instance:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Support\Collection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Collection&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;macro&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'customMethod'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Your custom method logic&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nv"&gt;$collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="c1"&gt;// use on any collection object&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$collection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;customMethod&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;concat()&lt;/strong&gt;: Suppose you have two collections of users from different sources and want to combine them into a single collection. You can use &lt;code&gt;concat&lt;/code&gt; for this purpose:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$usersFromDatabase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$usersFromApi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$combinedUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$usersFromDatabase&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$usersFromApi&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;pad()&lt;/strong&gt;: You have a collection of tasks, but you want to ensure that it always contains a minimum number of elements. You can use &lt;code&gt;pad&lt;/code&gt; to add dummy tasks if necessary:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$paddedTasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$tasks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;pad&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Dummy Task'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;shuffle()&lt;/strong&gt; &amp;amp; &lt;strong&gt;random()&lt;/strong&gt;: Suppose you have a quiz application and want to shuffle the order of the questions. You can use &lt;code&gt;shuffle&lt;/code&gt; for this purpose. Additionally, if you're going to select a question from the collection randomly, you can use &lt;code&gt;random&lt;/code&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$questions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$shuffledQuestions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$questions&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;shuffle&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$randomQuestion&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$questions&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;crossJoin()&lt;/strong&gt;: Suppose you've two collections and you want to generate all possible combinations from them.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
&lt;span class="nv"&gt;$matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$collection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;crossJoin&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
&lt;span class="nv"&gt;$matrix&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="cm"&gt;/*[
    [1, 'a'],
    [1, 'b'],
    [2, 'a'],
    [2, 'b'],
]*/&lt;/span&gt;

&lt;span class="nv"&gt;$collection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
&lt;span class="nv"&gt;$matrix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$collection&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;crossJoin&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'I'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'II'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; 
&lt;span class="nv"&gt;$matrix&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="cm"&gt;/*[
    [1, 'a', 'I'],
    [1, 'a', 'II'],
    [1, 'b', 'I'],
    [1, 'b', 'II'],
    [2, 'a', 'I'],
    [2, 'a', 'II'],
    [2, 'b', 'I'],
    [2, 'b', 'II'],
] */&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;partition()&lt;/strong&gt;: Imagine you have a collection of students, and you want to partition them into two groups based on their grades (pass or fail). &lt;code&gt;partition&lt;/code&gt; makes this easy:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="k"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$passingStudents&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$failingStudents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$students&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;partition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$student&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$student&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;grade&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;first()&lt;/strong&gt; and &lt;strong&gt;firstWhere()&lt;/strong&gt;: You have a collection of tasks, and you want to retrieve the first task or the first task that meets certain criteria. &lt;code&gt;first&lt;/code&gt; and &lt;code&gt;firstWhere&lt;/code&gt; come in handy:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$firstTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$tasks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nv"&gt;$urgentTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$tasks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstWhere&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'priority'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'urgent'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;keyBy()&lt;/strong&gt;: You have a collection of users, and you want to index them by their unique IDs for quick access. &lt;code&gt;keyBy&lt;/code&gt; is the solution:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$indexedUsers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$users&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;keyBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;filter()&lt;/strong&gt;: You have a collection of orders coming from API and you want to filter out the canceled orders. The &lt;code&gt;filter&lt;/code&gt; method is perfect for this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$orders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$validOrders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$orders&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'canceled'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;transform()&lt;/strong&gt;: You have a collection of tasks, and you want to modify each task in some way. &lt;code&gt;transform&lt;/code&gt; allows you to apply a callback to each item to replace it:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nv"&gt;$tasks&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$task&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$task&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;' - '&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$task&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;






&lt;p&gt;That's all for now, folks! These methods offer ease &amp;amp; flexibility that can be useful when working with Laravel applications. &lt;/p&gt;

&lt;p&gt;All the above have been previously shared on our Twitter, one by one. &lt;a href="https://twitter.com/laravelbackpack"&gt;Follow us on Twitter&lt;/a&gt;; You'll ❤️ it. You can also check the first article of the series, which is on &lt;a href="https://backpackforlaravel.com/articles/tips-and-tricks/laravel-advanced-top-5-scheduler-functions-you-might-not-know-about"&gt;Top 5 Scheduler Functions you might not know about&lt;/a&gt;. Keep exploring, keep coding, and keep pushing the boundaries of what you can achieve.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>programming</category>
    </item>
    <item>
      <title>Laravel Advanced: Lesser-Known, Yet Useful Composer Commands</title>
      <dc:creator>Karan Datwani</dc:creator>
      <pubDate>Sun, 02 Jun 2024 13:43:59 +0000</pubDate>
      <link>https://dev.to/karandatwani92/laravel-advanced-lesser-known-yet-useful-composer-commands-3l40</link>
      <guid>https://dev.to/karandatwani92/laravel-advanced-lesser-known-yet-useful-composer-commands-3l40</guid>
      <description>&lt;p&gt;Composer is the go-to dependency manager for PHP, and if you're working with Laravel, you're already familiar with frequently used commands like &lt;code&gt;composer install&lt;/code&gt; and &lt;code&gt;composer update&lt;/code&gt;. Composer also offers some commands that are lesser-known but helpful while working on your Laravel app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2F2SpGpWvk%2Fimage.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.postimg.cc%2F2SpGpWvk%2Fimage.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here are five Composer commands you might not know but would love to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;composer outdated&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Ever wondered which of your app dependencies are outdated? &lt;strong&gt;&lt;code&gt;composer outdated&lt;/code&gt;&lt;/strong&gt; gives you a quick rundown of all packages that have newer versions available. This is especially useful for keeping your project up-to-date and secure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer outdated
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This lists all the outdated packages in your project, showing the current and latest versions. It's a handy way to stay on top of updates without blindly running &lt;code&gt;composer update&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;code&gt;composer show&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Need an overview of the installed packages? The &lt;code&gt;composer show&lt;/code&gt; displays information about all the packages installed in your project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer show
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use it to get details about a specific package by passing the package name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer show vendor/package
&lt;span class="c"&gt;# Example: composer show backpack/crud&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s a great way to quickly check the installed version, description, and dependencies of any package in your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. &lt;code&gt;composer why&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Wanna figure out why a particular package is installed? &lt;code&gt;composer why&lt;/code&gt; helps you trace the dependency tree to understand which package requires it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer why vendor/package
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. &lt;code&gt;composer licenses&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Wanna know the licenses of the packages you are using? &lt;code&gt;composer licenses&lt;/code&gt; provides a summary of all the licenses of the installed dependencies. This is useful for ensuring compliance with open-source licenses.&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. &lt;code&gt;composer check-platform-reqs&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Checking all required PHP extensions are installed can be a hassle while working on a project across multiple environments. The &lt;code&gt;composer check-platform-reqs&lt;/code&gt; command checks if your platform meets the package requirements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer check-platform-reqs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command verifies that the PHP version and all required extensions are installed and meet the version constraints specified in your &lt;code&gt;composer.json&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While basic Composer commands get the job done, these lesser-known commands can save you time and hassle by giving you deeper insights and more control over your project's dependencies. So, the next time you fire up your terminal, try these commands and see how they can improve your Laravel experience.&lt;/p&gt;

&lt;p&gt;All of the above have been previously shared on our Twitter, one by one. &lt;a href="https://twitter.com/laravelbackpack" rel="noopener noreferrer"&gt;Follow us on Twitter&lt;/a&gt;; You'll ❤️ it. You can also check the first article of the series, which is on the &lt;a href="https://backpackforlaravel.com/articles/tips-and-tricks/laravel-advanced-top-5-scheduler-functions-you-might-not-know-about" rel="noopener noreferrer"&gt;Top 5 Scheduler Functions you might not know about&lt;/a&gt;. Keep exploring, and keep coding with ease using Laravel. Until next time, happy composing! 🚀&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>learning</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
