<?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: Laravel Daily tips</title>
    <description>The latest articles on DEV Community by Laravel Daily tips (@laravel_dailytips).</description>
    <link>https://dev.to/laravel_dailytips</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3270677%2F8eebade1-7a15-4cab-9df3-2e47254bd397.png</url>
      <title>DEV Community: Laravel Daily tips</title>
      <link>https://dev.to/laravel_dailytips</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/laravel_dailytips"/>
    <language>en</language>
    <item>
      <title>Laravel 13: Real-Time Notifications using Pusher</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Mon, 10 Aug 2026 23:05:13 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/laravel-13-real-time-notifications-using-pusher-4lc5</link>
      <guid>https://dev.to/laravel_dailytips/laravel-13-real-time-notifications-using-pusher-4lc5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Users expect modern web applications to respond instantly. Whether it's a new order arriving in an admin dashboard, a teammate sending a chat message, or a support ticket being created, waiting for users to refresh the page every few seconds no longer provides a good user experience.&lt;/p&gt;

&lt;p&gt;Laravel solves this problem through its powerful broadcasting system. Instead of repeatedly asking the server whether anything has changed, Laravel pushes updates to connected browsers the moment an event occurs.&lt;/p&gt;

&lt;p&gt;At the center of this system are Laravel Events. Whenever something important happens inside your application—such as publishing a post, creating an order, or processing a payment—you simply dispatch an event. Laravel then broadcasts that event through a broadcasting driver like Pusher or Laravel Reverb, allowing every subscribed client to react immediately.&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll build a practical notification system where a regular user creates a post and an administrator instantly receives a notification without refreshing the browser. Although the example is intentionally simple, the same architecture is used in production systems for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Order management dashboards&lt;/li&gt;
&lt;li&gt;Customer support platforms&lt;/li&gt;
&lt;li&gt;Messaging applications&lt;/li&gt;
&lt;li&gt;Inventory monitoring&lt;/li&gt;
&lt;li&gt;Payment processing&lt;/li&gt;
&lt;li&gt;Project management tools&lt;/li&gt;
&lt;li&gt;Activity feeds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More importantly, you'll learn why Laravel's broadcasting system exists, how each component communicates internally, and how to build similar real-time features in your own applications with confidence.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Are Real-Time Notifications?
&lt;/h2&gt;

&lt;p&gt;Real-time notifications allow information to appear in the browser immediately after an action occurs on the server.&lt;/p&gt;

&lt;p&gt;Instead of sending repeated HTTP requests asking whether new data exists, the server pushes updates only when something actually changes.&lt;/p&gt;

&lt;p&gt;Consider an e-commerce dashboard where customers continuously place new orders.&lt;/p&gt;

&lt;p&gt;Without broadcasting, the administrator has two choices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refresh the page manually every few seconds.&lt;/li&gt;
&lt;li&gt;Implement polling, where the browser repeatedly sends AJAX requests looking for updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both approaches waste resources and provide a less responsive user experience.&lt;/p&gt;

&lt;p&gt;With Laravel Broadcasting, the workflow becomes much more efficient.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A customer submits a new order.&lt;/li&gt;
&lt;li&gt;Laravel dispatches an event.&lt;/li&gt;
&lt;li&gt;The event is broadcast through Pusher.&lt;/li&gt;
&lt;li&gt;The administrator's browser receives the event immediately.&lt;/li&gt;
&lt;li&gt;The interface updates automatically.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Production Tip:&lt;/strong&gt; Broadcasting isn't limited to notifications. The same architecture can power live dashboards, chat systems, progress bars, collaborative editing, stock updates, analytics panels, and monitoring applications.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  How Laravel Broadcasting Works
&lt;/h2&gt;

&lt;p&gt;Before writing any code, it's worth understanding what happens internally whenever Laravel broadcasts an event.&lt;/p&gt;

&lt;p&gt;Every component has a single responsibility, making the system easy to maintain and extend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Creates Post
        │
        ▼
PostController
        │
        ▼
Dispatch PostCreated Event
        │
        ▼
Broadcast Driver
        │
        ▼
Pusher Channels
        │
        ▼
Connected Browsers
        │
        ▼
Laravel Echo
        │
        ▼
Update User Interface

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

&lt;/div&gt;



&lt;p&gt;The process may look complex initially, but Laravel abstracts almost all of the heavy lifting. Your application simply dispatches an event, while Laravel handles serialization, broadcasting, and communication with the broadcasting service.&lt;/p&gt;

&lt;p&gt;On the frontend, Laravel Echo maintains the WebSocket connection and listens for incoming events. Whenever an event arrives, JavaScript can update the page instantly without another HTTP request.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Laravel Uses Events for Broadcasting
&lt;/h2&gt;

&lt;p&gt;Developers often wonder why Laravel doesn't simply send JavaScript responses directly from the controller.&lt;/p&gt;

&lt;p&gt;The answer is separation of concerns.&lt;/p&gt;

&lt;p&gt;A controller should only coordinate a request. It validates incoming data, performs business logic, and returns a response. It shouldn't also manage WebSocket connections, notifications, emails, logging, or analytics.&lt;/p&gt;

&lt;p&gt;Instead, Laravel encourages an event-driven architecture.&lt;/p&gt;

&lt;p&gt;Once a post has been successfully created, the controller dispatches a &lt;strong&gt;PostCreated&lt;/strong&gt; event. That event becomes responsible for broadcasting information, while the frontend decides how to display it.&lt;/p&gt;

&lt;p&gt;This design offers several long-term advantages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Controllers remain small and focused.&lt;/li&gt;
&lt;li&gt;Broadcasting logic becomes reusable.&lt;/li&gt;
&lt;li&gt;Additional listeners can be added later without modifying existing controllers.&lt;/li&gt;
&lt;li&gt;Applications become easier to test.&lt;/li&gt;
&lt;li&gt;Future integrations such as emails, Slack notifications, or analytics can reuse the same event.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Many experienced Laravel developers treat events as a communication layer inside the application. Once business logic finishes, everything else—notifications, logging, broadcasting, emails, and third-party integrations—can react independently through listeners or broadcast events.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Why Choose Pusher?
&lt;/h2&gt;

&lt;p&gt;Pusher Channels is a hosted WebSocket service that works seamlessly with Laravel's broadcasting system. Instead of managing your own WebSocket infrastructure, Pusher handles persistent connections, message delivery, scaling, and connection management.&lt;/p&gt;

&lt;p&gt;This significantly reduces the amount of infrastructure you need to maintain while allowing you to focus on building application features.&lt;/p&gt;

&lt;p&gt;Some of its biggest advantages include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simple Laravel integration.&lt;/li&gt;
&lt;li&gt;No WebSocket server management.&lt;/li&gt;
&lt;li&gt;Reliable real-time message delivery.&lt;/li&gt;
&lt;li&gt;Automatic connection handling.&lt;/li&gt;
&lt;li&gt;Support for public, private, and presence channels.&lt;/li&gt;
&lt;li&gt;Scales well for SaaS products and business applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For many teams, Pusher is the quickest way to introduce real-time functionality without investing time in infrastructure management.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pusher vs Laravel Reverb
&lt;/h2&gt;

&lt;p&gt;Since Laravel introduced Reverb, developers now have two excellent choices for implementing WebSocket communication.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Pusher&lt;/th&gt;
&lt;th&gt;Laravel Reverb&lt;/th&gt;
&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Hosting&lt;/td&gt;
&lt;td&gt;Managed Cloud Service&lt;/td&gt;
&lt;td&gt;Self-hosted&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Setup&lt;/td&gt;
&lt;td&gt;Very Easy&lt;/td&gt;
&lt;td&gt;Requires Server Configuration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Maintenance&lt;/td&gt;
&lt;td&gt;Minimal&lt;/td&gt;
&lt;td&gt;You Maintain Infrastructure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scaling&lt;/td&gt;
&lt;td&gt;Handled by Pusher&lt;/td&gt;
&lt;td&gt;Your Responsibility&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best For&lt;/td&gt;
&lt;td&gt;Rapid Development&lt;/td&gt;
&lt;td&gt;Large Laravel Deployments&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If your goal is to learn broadcasting quickly or ship features rapidly, Pusher provides an excellent developer experience.&lt;/p&gt;

&lt;p&gt;If you prefer complete control over your infrastructure and are comfortable managing WebSocket servers, Laravel Reverb is a powerful modern alternative built by the Laravel team.&lt;/p&gt;




&lt;h2&gt;
  
  
  What We'll Build
&lt;/h2&gt;

&lt;p&gt;Throughout this guide, we'll create a small but production-inspired application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Regular users can publish posts.&lt;/li&gt;
&lt;li&gt;Administrators automatically receive real-time notifications.&lt;/li&gt;
&lt;li&gt;Notifications appear instantly without refreshing the page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Although we're using posts to keep the tutorial easy to follow, the same implementation can be adapted for customer registrations, support tickets, order processing, appointment bookings, payment updates, or any other event that benefits from real-time communication.&lt;/p&gt;

&lt;p&gt;Rather than simply copying code, we'll explain why each command is necessary, how every component fits into Laravel's broadcasting ecosystem, and what you should consider when implementing similar features in production.&lt;/p&gt;




&lt;h2&gt;
  
  
  Project Structure and Prerequisites
&lt;/h2&gt;

&lt;p&gt;Before writing any code, let's look at the technologies used throughout this tutorial.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Laravel 13&lt;/li&gt;
&lt;li&gt;PHP 8.4 or later&lt;/li&gt;
&lt;li&gt;MySQL or MariaDB&lt;/li&gt;
&lt;li&gt;Pusher Channels&lt;/li&gt;
&lt;li&gt;Laravel Echo&lt;/li&gt;
&lt;li&gt;Vite&lt;/li&gt;
&lt;li&gt;Laravel Breeze (recommended) or Laravel UI&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll also create two application roles:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Administrator&lt;/strong&gt; — receives real-time notifications.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Regular User&lt;/strong&gt; — creates new posts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever a regular user publishes a post, Laravel dispatches a broadcast event, Pusher delivers the event to connected browsers, and Laravel Echo updates the administrator's interface instantly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Although this tutorial demonstrates a simple notification system, the architecture scales well to enterprise applications handling thousands of broadcast events every minute.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 1: Create a New Laravel Project
&lt;/h2&gt;

&lt;p&gt;If you already have an existing Laravel application, you can skip this step. Otherwise, create a fresh Laravel project using Composer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer create-project laravel/laravel realtime-notifications

&lt;span class="nb"&gt;cd &lt;/span&gt;realtime-notifications

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

&lt;/div&gt;



&lt;p&gt;Next, configure your database credentials inside your &lt;strong&gt;.env&lt;/strong&gt; file and run the default migrations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan migrate

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

&lt;/div&gt;



&lt;p&gt;This creates Laravel's default database tables, including the &lt;strong&gt;users&lt;/strong&gt; table that we'll extend later.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2: Install Authentication
&lt;/h2&gt;

&lt;p&gt;Since our example requires two different users, we'll add authentication before implementing broadcasting.&lt;/p&gt;

&lt;p&gt;Laravel Breeze is the recommended starter kit for modern Laravel applications because it's actively maintained and follows Laravel's latest frontend conventions.&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 laravel/breeze &lt;span class="nt"&gt;--dev&lt;/span&gt;

php artisan breeze:install

npm &lt;span class="nb"&gt;install

&lt;/span&gt;npm run build

php artisan migrate

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

&lt;/div&gt;



&lt;p&gt;After installation, your application includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User registration&lt;/li&gt;
&lt;li&gt;Login and logout functionality&lt;/li&gt;
&lt;li&gt;Password hashing&lt;/li&gt;
&lt;li&gt;Authentication middleware&lt;/li&gt;
&lt;li&gt;Modern Blade views&lt;/li&gt;
&lt;li&gt;Vite asset compilation&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Although older tutorials still use Laravel UI, Breeze is the preferred authentication starter kit for Laravel 11, Laravel 12, and Laravel 13. The broadcasting concepts in this guide remain exactly the same regardless of the starter kit you choose.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 3: Prepare the Database
&lt;/h2&gt;

&lt;p&gt;Our application requires two small database changes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add an &lt;strong&gt;is_admin&lt;/strong&gt; column to the users table.&lt;/li&gt;
&lt;li&gt;Create a &lt;strong&gt;posts&lt;/strong&gt; table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Generate the migrations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:migration add_is_admin_to_users_table

php artisan make:migration create_posts_table

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

&lt;/div&gt;



&lt;p&gt;Update the users migration.&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="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nc"&gt;Schema&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;'users'&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="kt"&gt;Blueprint&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'is_admin'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create the posts table.&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="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="nc"&gt;Schema&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&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="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Blueprint&lt;/span&gt; &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;foreignId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user_id'&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;constrained&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;cascadeOnDelete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;string&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="nv"&gt;$table&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;'body'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nv"&gt;$table&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;timestamps&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;The foreign key ensures every post belongs to a valid user, while cascading deletes automatically remove related posts if a user is deleted.&lt;/p&gt;

&lt;p&gt;Finally, execute the migrations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan migrate

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 4: Create the Post Model
&lt;/h2&gt;

&lt;p&gt;Our migration created the database table, but we still need an Eloquent model to interact with it.&lt;/p&gt;

&lt;p&gt;Create the model using Artisan.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:model Post

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

&lt;/div&gt;



&lt;p&gt;Now update the model.&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="cp"&gt;&amp;lt;?php&lt;/span&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\Database\Eloquent\Relations\BelongsTo&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;'body'&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;function&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;BelongsTo&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;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;belongsTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;p&gt;The &lt;strong&gt;$fillable&lt;/strong&gt; property protects your application against mass-assignment vulnerabilities by explicitly defining which attributes may be assigned.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;user()&lt;/strong&gt; relationship tells Laravel that every post belongs to exactly one user.&lt;/p&gt;

&lt;p&gt;Later in the article, this relationship allows us to write expressive code like:&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;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;instead of manually writing additional database queries.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5: Add the User Relationship
&lt;/h2&gt;

&lt;p&gt;The controller will create posts through the authenticated user, so the &lt;strong&gt;User&lt;/strong&gt; model also needs a relationship.&lt;/p&gt;

&lt;p&gt;Open &lt;strong&gt;app/Models/User.php&lt;/strong&gt; and add the following method.&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="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Database\Eloquent\Relations\HasMany&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;posts&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;HasMany&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;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hasMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&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;This relationship allows Laravel to automatically associate newly created posts with the currently authenticated user.&lt;/p&gt;

&lt;p&gt;Instead of writing:&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;$post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
    &lt;span class="s1"&gt;'user_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;auth&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;id&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$validated&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;'body'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$validated&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'body'&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;you can write the much cleaner and more expressive:&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;$post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;auth&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;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;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;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$validated&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Laravel automatically fills the &lt;strong&gt;user_id&lt;/strong&gt; column using the relationship.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Using Eloquent relationships keeps your code more readable and reduces the chance of accidentally assigning incorrect foreign keys.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 6: Configure Broadcasting
&lt;/h2&gt;

&lt;p&gt;Laravel's broadcasting features aren't enabled in a fresh installation.&lt;/p&gt;

&lt;p&gt;Fortunately, modern Laravel versions provide an Artisan command that performs most of the setup automatically.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan &lt;span class="nb"&gt;install&lt;/span&gt;:broadcasting

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

&lt;/div&gt;



&lt;p&gt;This command creates the required broadcasting configuration and generates an &lt;strong&gt;echo.js&lt;/strong&gt; file inside your JavaScript resources.&lt;/p&gt;

&lt;p&gt;Next, install Pusher's PHP SDK.&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 pusher/pusher-php-server

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

&lt;/div&gt;



&lt;p&gt;Laravel also needs the frontend libraries responsible for maintaining the WebSocket connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;laravel-echo pusher-js

npm run build

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 7: Create a Pusher Application
&lt;/h2&gt;

&lt;p&gt;Create a new application from your Pusher Channels dashboard.&lt;/p&gt;

&lt;p&gt;Pusher will provide four credentials:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;App ID&lt;/li&gt;
&lt;li&gt;App Key&lt;/li&gt;
&lt;li&gt;App Secret&lt;/li&gt;
&lt;li&gt;Cluster&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Add them to your &lt;strong&gt;.env&lt;/strong&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="py"&gt;BROADCAST_CONNECTION&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;pusher&lt;/span&gt;

&lt;span class="py"&gt;PUSHER_APP_ID&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;your-app-id&lt;/span&gt;
&lt;span class="py"&gt;PUSHER_APP_KEY&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;your-app-key&lt;/span&gt;
&lt;span class="py"&gt;PUSHER_APP_SECRET&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;your-app-secret&lt;/span&gt;
&lt;span class="py"&gt;PUSHER_APP_CLUSTER&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;ap2&lt;/span&gt;

&lt;span class="py"&gt;VITE_PUSHER_APP_KEY&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"${PUSHER_APP_KEY}"&lt;/span&gt;
&lt;span class="py"&gt;VITE_PUSHER_APP_CLUSTER&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"${PUSHER_APP_CLUSTER}"&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Never commit Pusher credentials to Git. Store them only in environment variables and use different credentials for local, staging, and production environments.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 8: Configure Laravel Echo
&lt;/h2&gt;

&lt;p&gt;Laravel generates an &lt;strong&gt;echo.js&lt;/strong&gt; file after installing broadcasting.&lt;/p&gt;

&lt;p&gt;Update it as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Echo&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;laravel-echo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Pusher&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pusher-js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Pusher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Pusher&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Echo&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;Echo&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;broadcaster&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;pusher&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VITE_PUSHER_APP_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;cluster&lt;/span&gt;&lt;span class="p"&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VITE_PUSHER_APP_CLUSTER&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;forceTLS&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;wsHost&lt;/span&gt;&lt;span class="p"&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VITE_PUSHER_HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;wsPort&lt;/span&gt;&lt;span class="p"&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VITE_PUSHER_PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;wssPort&lt;/span&gt;&lt;span class="p"&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;VITE_PUSHER_PORT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;enabledTransports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ws&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wss&lt;/span&gt;&lt;span class="dl"&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;p&gt;One step that's commonly missed in tutorials is importing this file into your application's main JavaScript entry point.&lt;/p&gt;

&lt;p&gt;Open &lt;strong&gt;resources/js/app.js&lt;/strong&gt; and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./echo&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Without importing &lt;strong&gt;echo.js&lt;/strong&gt;, Laravel Echo never initializes, meaning your browser won't subscribe to any broadcast channels.&lt;/p&gt;

&lt;p&gt;Finally, rebuild your frontend assets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run build

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Project Structure So Far
&lt;/h2&gt;

&lt;p&gt;At this stage, your application should look similar to the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app
├── Events
├── Http
│   └── Controllers
├── Models
│   ├── Post.php
│   └── User.php
│
resources
└── js
    ├── app.js
    └── echo.js

routes
├── web.php
└── channels.php

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

&lt;/div&gt;



&lt;p&gt;Having this structure in place makes the remaining implementation much easier to follow. In the next section, we'll create the broadcast event, build the controller, dispatch the event after saving a post, subscribe to the broadcast channel with Laravel Echo, and display real-time notifications inside the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Create the Broadcast Event
&lt;/h2&gt;

&lt;p&gt;Now that the project is configured, it's time to create the event responsible for broadcasting newly created posts.&lt;/p&gt;

&lt;p&gt;Instead of sending WebSocket messages directly from the controller, Laravel encourages using events. This keeps your business logic clean while allowing multiple parts of the application to react independently.&lt;/p&gt;

&lt;p&gt;Generate the event using Artisan.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:event PostCreated

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

&lt;/div&gt;



&lt;p&gt;Update the generated class as shown below.&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="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Events&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;App\Models\Post&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\Broadcasting\Channel&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\Contracts\Broadcasting\ShouldBroadcast&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\Foundation\Events\Dispatchable&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\Queue\SerializesModels&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;PostCreated&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ShouldBroadcast&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;Dispatchable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SerializesModels&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;Post&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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;broadcastOn&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;Channel&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;new&lt;/span&gt; &lt;span class="nc"&gt;Channel&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="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;broadcastAs&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'post.created'&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;function&lt;/span&gt; &lt;span class="n"&gt;broadcastWith&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;array&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="s1"&gt;'id'&lt;/span&gt;         &lt;span class="o"&gt;=&amp;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;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="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'title'&lt;/span&gt;      &lt;span class="o"&gt;=&amp;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;post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'author'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;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;post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;user&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="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'created_at'&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toDateTimeString&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;p&gt;Notice that the event broadcasts only the data the frontend actually needs instead of exposing the complete &lt;strong&gt;Post&lt;/strong&gt; model.&lt;/p&gt;

&lt;p&gt;This keeps payloads smaller, reduces serialization overhead, and avoids accidentally exposing sensitive information.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why use ShouldBroadcast instead of ShouldBroadcastNow?&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;For production applications, &lt;strong&gt;ShouldBroadcast&lt;/strong&gt; is usually the better choice because the event is processed through Laravel's queue system. This keeps HTTP requests fast even when your application broadcasts thousands of events. Use &lt;strong&gt;ShouldBroadcastNow&lt;/strong&gt; only when immediate delivery is absolutely necessary.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 10: Define the Application Routes
&lt;/h2&gt;

&lt;p&gt;Next, create the routes in &lt;strong&gt;web.php&lt;/strong&gt; that responsible for displaying posts and storing newly created ones.&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="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Http\Controllers\PostController&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\Route&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'auth'&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;group&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="nc"&gt;Route&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;'/posts'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;PostController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'index'&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;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'posts.index'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;post&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="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;PostController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'store'&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;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'posts.store'&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;Both routes are protected by the &lt;strong&gt;auth&lt;/strong&gt; middleware, ensuring that only authenticated users can create posts or receive notifications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 11: Build the Controller
&lt;/h2&gt;

&lt;p&gt;Create the controller if it doesn't already exist.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:controller PostController

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

&lt;/div&gt;



&lt;p&gt;Update the controller with the following implementation.&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="cp"&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Http\Controllers&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;App\Events\PostCreated&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;App\Models\Post&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\Http\Request&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;PostController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&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;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'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;latest&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="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'posts.index'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;compact&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="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;store&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$validated&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
            &lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'required'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'max:255'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
            &lt;span class="s1"&gt;'body'&lt;/span&gt;  &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'required'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'string'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;]);&lt;/span&gt;

        &lt;span class="nv"&gt;$post&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;auth&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;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;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;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$validated&lt;/span&gt;&lt;span class="p"&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="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nf"&gt;broadcast&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;PostCreated&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toOthers&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;redirect&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;back&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;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'success'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Post created successfully.'&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;p&gt;Several production-ready improvements are worth mentioning.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Validation happens before any database operation.&lt;/li&gt;
&lt;li&gt;The post is created through the authenticated user's relationship.&lt;/li&gt;
&lt;li&gt;The related user is eager loaded before broadcasting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;broadcast()-&amp;gt;toOthers()&lt;/strong&gt; prevents duplicate notifications in the same browser session.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Loading the relationship before broadcasting prevents additional database queries while Laravel serializes the event payload.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why &lt;code&gt;toOthers()&lt;/code&gt; Matters
&lt;/h2&gt;

&lt;p&gt;This is one of the most misunderstood parts of Laravel Broadcasting.&lt;/p&gt;

&lt;p&gt;Without calling:&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="nf"&gt;broadcast&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;PostCreated&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the browser that created the post will also receive the broadcast event.&lt;/p&gt;

&lt;p&gt;That usually results in duplicate UI updates because the user already knows they created the post.&lt;/p&gt;

&lt;p&gt;Instead, Laravel allows you to exclude the current WebSocket connection.&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="nf"&gt;broadcast&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;PostCreated&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toOthers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Laravel automatically uses the current socket ID supplied by Laravel Echo to exclude that browser connection while still notifying every other connected client.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Many developers think &lt;strong&gt;toOthers()&lt;/strong&gt; excludes the authenticated user. It doesn't. It excludes only the current browser connection. If the same user has your application open in another browser or another device, those sessions will still receive the event.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Step 12: Listen for Events with Laravel Echo
&lt;/h2&gt;

&lt;p&gt;Broadcasting an event is only half of the process. The browser must also subscribe to the channel and react whenever a new event arrives.&lt;/p&gt;

&lt;p&gt;Laravel Echo makes this extremely straightforward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Echo&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.post.created&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nf"&gt;showNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;Whenever Laravel broadcasts the &lt;strong&gt;PostCreated&lt;/strong&gt; event, every subscribed browser immediately receives the payload returned by &lt;strong&gt;broadcastWith()&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;At this point, the browser can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Display toast notifications.&lt;/li&gt;
&lt;li&gt;Append new rows to a table.&lt;/li&gt;
&lt;li&gt;Refresh dashboard statistics.&lt;/li&gt;
&lt;li&gt;Update charts.&lt;/li&gt;
&lt;li&gt;Increment notification badges.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No additional HTTP request is required.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 13: Build a Reusable Notification Function
&lt;/h2&gt;

&lt;p&gt;Many beginner tutorials inject HTML directly inside the Echo listener.&lt;/p&gt;

&lt;p&gt;While this works for demonstrations, it becomes difficult to maintain as your application grows.&lt;/p&gt;

&lt;p&gt;A cleaner approach is to move rendering into its own function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;showNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;container&lt;/span&gt; &lt;span class="o"&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="s1"&gt;notifications&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;container&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertAdjacentHTML&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;afterbegin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;`
            &amp;lt;div class="alert alert-success mb-2"&amp;gt;

                &amp;lt;strong&amp;gt;New Post Published&amp;lt;/strong&amp;gt;

                &amp;lt;p class="mb-0"&amp;gt;

                    &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; published
                    "&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"

                &amp;lt;/p&amp;gt;

            &amp;lt;/div&amp;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;p&gt;Separating rendering logic makes it much easier to replace Bootstrap alerts with Toast notifications, Alpine.js components, Livewire, Vue, or React later without changing your broadcasting code.&lt;/p&gt;




&lt;h2&gt;
  
  
  What Should the Blade View Contain?
&lt;/h2&gt;

&lt;p&gt;Since the focus of this article is broadcasting rather than frontend development, we won't build the complete Blade template. However, your &lt;strong&gt;posts/index.blade.php&lt;/strong&gt; page should include the following elements.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A form for creating new posts.&lt;/li&gt;
&lt;li&gt;A container with the ID &lt;strong&gt;notifications&lt;/strong&gt; where real-time alerts will appear.&lt;/li&gt;
&lt;li&gt;A list of previously published posts.&lt;/li&gt;
&lt;li&gt;Your compiled JavaScript assets using Vite.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As long as those elements exist, the broadcasting example shown throughout this guide will work correctly regardless of whether you're using Bootstrap, Tailwind CSS, Livewire, Alpine.js, Vue, or React.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This tutorial intentionally keeps the frontend implementation minimal so you can adapt the broadcasting logic to your preferred frontend stack without rewriting your backend.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Testing the Complete Workflow
&lt;/h2&gt;

&lt;p&gt;At this point, the application is ready for testing.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open two different browsers or use an incognito window.&lt;/li&gt;
&lt;li&gt;Login as an administrator in the first browser.&lt;/li&gt;
&lt;li&gt;Login as a regular user in the second browser.&lt;/li&gt;
&lt;li&gt;Navigate both users to the posts page.&lt;/li&gt;
&lt;li&gt;Create a new post using the regular user's account.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If everything has been configured correctly, the workflow should look like this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The post is stored in the database.&lt;/li&gt;
&lt;li&gt;Laravel dispatches the &lt;strong&gt;PostCreated&lt;/strong&gt; event.&lt;/li&gt;
&lt;li&gt;The event is queued and broadcast to Pusher.&lt;/li&gt;
&lt;li&gt;Pusher delivers the event to connected clients.&lt;/li&gt;
&lt;li&gt;Laravel Echo receives the event.&lt;/li&gt;
&lt;li&gt;The administrator immediately sees the notification without refreshing the page.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You've now built a complete real-time notification flow using Laravel Broadcasting, Pusher, and Laravel Echo. In the final phase, we'll cover debugging strategies, performance optimization, production best practices, comparisons, FAQs, SEO recommendations, screenshot suggestions, and the finishing touches that make this guide production-ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  Complete Source Code
&lt;/h2&gt;

&lt;p&gt;If you'd like to compare your implementation with a working example or prefer to start from a complete project, the full source code used in this tutorial is available on GitHub.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;GitHub Repository:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://github.com/fadi06/realtime-notifications-with-pusher" rel="noopener noreferrer"&gt; https://github.com/fadi06/realtime-notifications-with-pusher &lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The repository includes the complete Laravel application with broadcasting configured, Pusher integration, event broadcasting, Laravel Echo setup, and real-time notifications. You can clone it locally to follow along with the article or use it as a reference if you encounter any issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Broadcasting Problems and Their Solutions
&lt;/h2&gt;

&lt;p&gt;Broadcasting involves several moving parts, including Laravel, Pusher, Laravel Echo, queues, and JavaScript. A small configuration mistake in any one of these can prevent events from reaching the browser.&lt;/p&gt;

&lt;p&gt;The good news is that most broadcasting issues are straightforward to diagnose once you know where to look.&lt;/p&gt;




&lt;h3&gt;
  
  
  Events Never Reach the Browser
&lt;/h3&gt;

&lt;p&gt;If your controller executes successfully but no notification appears, verify the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your &lt;strong&gt;.env&lt;/strong&gt; contains the correct Pusher credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BROADCAST_CONNECTION&lt;/strong&gt; is set to &lt;strong&gt;pusher&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Your queue worker is running when using &lt;strong&gt;ShouldBroadcast&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You rebuilt frontend assets after editing &lt;strong&gt;echo.js&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The browser successfully connects to Pusher.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Whenever you update environment variables, remember to clear Laravel's cached configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan optimize:clear

php artisan config:clear

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

&lt;/div&gt;






&lt;h3&gt;
  
  
  Queue Worker Isn't Running
&lt;/h3&gt;

&lt;p&gt;If your event implements &lt;strong&gt;ShouldBroadcast&lt;/strong&gt;, Laravel sends the broadcast through the queue system.&lt;/p&gt;

&lt;p&gt;Without an active queue worker, your events remain in the queue and never reach Pusher.&lt;/p&gt;

&lt;p&gt;Start a worker using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// .env
&lt;span class="nv"&gt;QUEUE_CONNECTION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;database

// run &lt;span class="k"&gt;in &lt;/span&gt;terminal
php artisan queue:work

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

&lt;/div&gt;



&lt;p&gt;Many developers mistakenly believe broadcasting is broken when the real issue is simply that no queue worker is processing jobs.&lt;/p&gt;




&lt;h3&gt;
  
  
  Laravel Echo Doesn't Connect
&lt;/h3&gt;

&lt;p&gt;Open your browser's Developer Tools and inspect the Network and Console tabs.&lt;/p&gt;

&lt;p&gt;Common causes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Incorrect App Key.&lt;/li&gt;
&lt;li&gt;Wrong Pusher Cluster.&lt;/li&gt;
&lt;li&gt;Mixed HTTP and HTTPS configuration.&lt;/li&gt;
&lt;li&gt;WebSocket connections blocked by a firewall or proxy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;echo.js&lt;/strong&gt; wasn't imported into &lt;strong&gt;app.js&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If Echo never connects, no broadcast event will ever arrive regardless of how correctly your Laravel backend is configured.&lt;/p&gt;




&lt;h3&gt;
  
  
  Events Fire Twice
&lt;/h3&gt;

&lt;p&gt;This usually happens because the browser that created the post also receives the broadcast.&lt;/p&gt;

&lt;p&gt;Use:&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="nf"&gt;broadcast&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;PostCreated&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toOthers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;to prevent duplicate notifications for the current browser session.&lt;/p&gt;




&lt;h3&gt;
  
  
  Private Channels Return 403 Errors
&lt;/h3&gt;

&lt;p&gt;If you later replace the public channel with a private channel, Laravel authorizes every subscription before allowing users to receive events.&lt;/p&gt;

&lt;p&gt;Most authorization failures occur because the callback inside &lt;strong&gt;routes/channels.php&lt;/strong&gt; hasn't been configured correctly.&lt;/p&gt;

&lt;p&gt;Always verify channel authorization before debugging JavaScript.&lt;/p&gt;




&lt;h2&gt;
  
  
  Performance Considerations
&lt;/h2&gt;

&lt;p&gt;Broadcasting is lightweight, but large applications can generate thousands of events every minute. A few architectural decisions can dramatically improve scalability.&lt;/p&gt;

&lt;h3&gt;
  
  
  Broadcast Only the Data You Need
&lt;/h3&gt;

&lt;p&gt;Avoid broadcasting complete Eloquent models.&lt;/p&gt;

&lt;p&gt;Instead of:&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;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'post'&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;post&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;Prefer:&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;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'id'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;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;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="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'title'&lt;/span&gt;  &lt;span class="o"&gt;=&amp;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;post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'author'&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;user&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="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;Smaller payloads reduce serialization time, improve transmission speed, and consume less bandwidth.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Queued Broadcasting
&lt;/h3&gt;

&lt;p&gt;Queued broadcasts prevent users from waiting while Laravel communicates with external services.&lt;/p&gt;

&lt;p&gt;For almost every production application, prefer &lt;strong&gt;ShouldBroadcast&lt;/strong&gt; over &lt;strong&gt;ShouldBroadcastNow&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Eager Load Relationships
&lt;/h3&gt;

&lt;p&gt;If your broadcast payload references related models, eager load them before dispatching the event.&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;$post&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'user'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This avoids additional database queries while Laravel serializes the event.&lt;/p&gt;

&lt;h3&gt;
  
  
  Choose the Right Channel Type
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;th&gt;Channel Type&lt;/th&gt;
&lt;th&gt;Use Case&lt;/th&gt;
&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Public Channel&lt;/td&gt;
&lt;td&gt;Public dashboards and announcements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Private Channel&lt;/td&gt;
&lt;td&gt;User-specific notifications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Presence Channel&lt;/td&gt;
&lt;td&gt;Chat rooms and online user lists&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For demonstration purposes, this tutorial uses a public channel. In production, notifications that contain user-specific information should almost always use private channels.&lt;/p&gt;




&lt;h2&gt;
  
  
  Pusher vs Traditional Polling
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Pusher Broadcasting&lt;/th&gt;
&lt;th&gt;Traditional Polling&lt;/th&gt;
&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Updates&lt;/td&gt;
&lt;td&gt;Instant&lt;/td&gt;
&lt;td&gt;Delayed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server Requests&lt;/td&gt;
&lt;td&gt;Only when events occur&lt;/td&gt;
&lt;td&gt;Continuous HTTP requests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server Load&lt;/td&gt;
&lt;td&gt;Lower&lt;/td&gt;
&lt;td&gt;Higher&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User Experience&lt;/td&gt;
&lt;td&gt;Excellent&lt;/td&gt;
&lt;td&gt;Depends on polling interval&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best For&lt;/td&gt;
&lt;td&gt;Live dashboards, chat, notifications&lt;/td&gt;
&lt;td&gt;Occasional updates&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If updates occur only once every few hours, polling is often sufficient. However, applications requiring immediate feedback benefit significantly from broadcasting.&lt;/p&gt;




&lt;h2&gt;
  
  
  Broadcast Events vs Laravel Notifications
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Broadcast Events&lt;/th&gt;
&lt;th&gt;Laravel Notifications&lt;/th&gt;
&lt;/tr&gt;&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary Purpose&lt;/td&gt;
&lt;td&gt;Real-time UI updates&lt;/td&gt;
&lt;td&gt;User notifications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Email Support&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database Notifications&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Real-time Support&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes (Broadcast Channel)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Best Choice&lt;/td&gt;
&lt;td&gt;Updating interfaces instantly&lt;/td&gt;
&lt;td&gt;Multi-channel communication&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Broadcast events and Laravel Notifications aren't competitors. In many production systems, notifications themselves are broadcast so users receive them instantly while also storing them in the database.&lt;/p&gt;




&lt;h2&gt;
  
  
  Production Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Keep controllers focused on business logic.&lt;/li&gt;
&lt;li&gt;Broadcast only the minimum required data.&lt;/li&gt;
&lt;li&gt;Prefer queued broadcasting.&lt;/li&gt;
&lt;li&gt;Use private channels for sensitive information.&lt;/li&gt;
&lt;li&gt;Never expose secrets inside broadcast payloads.&lt;/li&gt;
&lt;li&gt;Monitor failed queue jobs.&lt;/li&gt;
&lt;li&gt;Log broadcasting failures.&lt;/li&gt;
&lt;li&gt;Reuse events throughout your application whenever possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;As applications grow, broadcasting often becomes part of the application's core architecture. Investing time in clean event design, proper authorization, and efficient payloads will pay dividends as your application scales.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Frequently Asked Questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does Laravel support broadcasting by default?
&lt;/h3&gt;

&lt;p&gt;Yes. Laravel includes a built-in broadcasting system that integrates seamlessly with Pusher, Laravel Reverb, Ably, and other broadcasting drivers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I use Pusher or Laravel Reverb?
&lt;/h3&gt;

&lt;p&gt;Pusher is ideal if you want the quickest setup with minimal infrastructure management. Laravel Reverb is better suited for teams that prefer hosting their own WebSocket server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can Livewire receive broadcast events?
&lt;/h3&gt;

&lt;p&gt;Yes. Livewire integrates well with Laravel Echo, allowing components to refresh automatically whenever broadcast events are received.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I broadcast to a single user?
&lt;/h3&gt;

&lt;p&gt;Absolutely. Private channels allow Laravel to send events only to authorized users.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is queued broadcasting required?
&lt;/h3&gt;

&lt;p&gt;No, but it's highly recommended for production applications because it improves response times and scales much better under heavy traffic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I replace Pusher later?
&lt;/h3&gt;

&lt;p&gt;Yes. Because Laravel abstracts broadcasting through drivers, migrating from Pusher to Laravel Reverb or another supported provider typically requires only configuration changes.&lt;/p&gt;




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

&lt;p&gt;Real-time functionality has become a standard expectation in modern web applications. Whether you're building an admin dashboard, messaging platform, support system, or SaaS product, users expect updates to appear immediately without refreshing the page.&lt;/p&gt;

&lt;p&gt;Laravel's broadcasting system makes this remarkably approachable. By combining Events, Broadcasting, Laravel Echo, and Pusher, you can build responsive applications that keep users informed the moment something important happens.&lt;/p&gt;

&lt;p&gt;Although this tutorial demonstrated a simple post notification system, the same architecture applies to live order tracking, payment updates, collaborative editing, monitoring dashboards, and countless other real-world scenarios.&lt;/p&gt;

&lt;p&gt;As your application grows, focus on clean architecture as much as real-time functionality. Keep controllers small, broadcast only essential data, secure sensitive channels with proper authorization, and leverage queues to ensure your application remains responsive under load.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Laravel Broadcasting enables real-time communication between your application and connected browsers.&lt;/li&gt;
&lt;li&gt;Pusher provides one of the easiest ways to implement WebSocket functionality.&lt;/li&gt;
&lt;li&gt;Laravel Echo automatically listens for broadcast events on the frontend.&lt;/li&gt;
&lt;li&gt;Events help separate business logic from real-time communication.&lt;/li&gt;
&lt;li&gt;Queued broadcasting improves scalability and response times.&lt;/li&gt;
&lt;li&gt;Broadcast only the data required by the frontend.&lt;/li&gt;
&lt;li&gt;Private channels should be used for user-specific notifications.&lt;/li&gt;
&lt;li&gt;Laravel Reverb offers an excellent self-hosted alternative to Pusher.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Author's Note:&lt;/strong&gt; Pusher is one of the fastest ways to introduce real-time functionality into a Laravel application. Once you're comfortable with Laravel's broadcasting architecture, transitioning to Laravel Reverb or another broadcasting driver is usually a matter of configuration rather than rewriting your application.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>livewire</category>
    </item>
    <item>
      <title>12 Must-Know Laravel Tips from July 2025</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Wed, 06 Aug 2025 14:23:20 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/12-must-know-laravel-tips-from-july-2025-1ocj</link>
      <guid>https://dev.to/laravel_dailytips/12-must-know-laravel-tips-from-july-2025-1ocj</guid>
      <description>&lt;p&gt;This blog curates 12 practical Laravel and PHP tips shared by the developer community on X (formerly Twitter) in July 2025. From avoiding ambiguous column errors in Eloquent scopes to new features like &lt;code&gt;whereValueBetween()&lt;/code&gt; and secure password generation with &lt;code&gt;Str::password()&lt;/code&gt;, these insights can help improve your day-to-day Laravel development. Each tip is explained with a use-case example, making this blog a valuable resource for Laravel developers of all levels.&lt;/p&gt;

&lt;p&gt;Every month, the Laravel community on Twitter (or X) shares a ton of useful tips and tricks. This post gathers some of the best insights from July, covering everything from Eloquent to Carbon and API responses. Let's dive into 12 tips that can make you a more efficient Laravel developer&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Eloquent Scopes with &lt;code&gt;getTable()&lt;/code&gt; for Ambiguous Columns
&lt;/h3&gt;

&lt;p&gt;When you're working with database tables that have columns with the same name (like &lt;code&gt;is_displayed&lt;/code&gt;), you might run into an "ambiguous column" SQL error during joins. A simple way to avoid this is to prefix the column name with the table name.&lt;/p&gt;

&lt;p&gt;Instead of writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;public&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;scopeIsDisplayed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;Builder&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;query&lt;/span&gt;&lt;span class="o"&gt;):&lt;/span&gt; &lt;span class="nt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;$query-&amp;gt;where('is_displayed',&lt;/span&gt; &lt;span class="err"&gt;1);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;public&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;scopeIsDisplayed&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;Builder&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;query&lt;/span&gt;&lt;span class="o"&gt;):&lt;/span&gt; &lt;span class="nt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;$query-&amp;gt;where($this-&amp;gt;getTable()&lt;/span&gt; &lt;span class="err"&gt;.&lt;/span&gt; &lt;span class="err"&gt;'.is_displayed',&lt;/span&gt; &lt;span class="err"&gt;1);&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;$this-&amp;gt;getTable()&lt;/code&gt; method dynamically gets the current model's table name, ensuring the query is explicit and error-free. An alternative method is &lt;code&gt;qualifyColumn()&lt;/code&gt;, but &lt;code&gt;getTable()&lt;/code&gt; is often considered more readable.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Carbon's &lt;code&gt;is...&lt;/code&gt; Methods
&lt;/h3&gt;

&lt;p&gt;The Carbon library, a standard part of Laravel, has an extensive list of helpful methods for comparing dates. Instead of manually checking if a date is today, tomorrow, or a weekday, you can use Carbon's built-in &lt;code&gt;is...&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;A few examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;$date-&amp;gt;isToday()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$date-&amp;gt;isTomorrow()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$date-&amp;gt;isYesterday()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$date-&amp;gt;isWeekend()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$date-&amp;gt;isWeekday()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$date-&amp;gt;isSameDay($otherDate)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before writing your own date comparison logic, check the Carbon documentation, it likely has a method for what you need.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Response Macros for Consistent APIs
&lt;/h3&gt;

&lt;p&gt;You can create consistent API responses across your application using &lt;strong&gt;Response Macros&lt;/strong&gt;. While a base controller or a trait is a common approach, macros offer a clean way to standardize responses directly in a service provider.&lt;/p&gt;

&lt;p&gt;First, define a macro in your &lt;code&gt;AppServiceProvider&lt;/code&gt; (or a dedicated service provider):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;Providers&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;ResponseMacroServiceProvider&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt;
&lt;span class="nt"&gt;public&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;boot&lt;/span&gt;&lt;span class="o"&gt;():&lt;/span&gt; &lt;span class="nt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;macro&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'apiSuccess'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;'Success'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;statusCode&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="m"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
            &lt;span class="s2"&gt;'success'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;'message'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;'data'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, you can use this macro in any of your controllers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;return&lt;/span&gt; &lt;span class="nt"&gt;response&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;apiSuccess&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'User created successfully.'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This centralizes your response logic, making it easy to maintain a consistent API.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;code&gt;in_array_keys&lt;/code&gt; Validation Rule
&lt;/h3&gt;

&lt;p&gt;Laravel &lt;code&gt;12.29&lt;/code&gt; introduced a new validation rule: &lt;code&gt;in_array_keys&lt;/code&gt;. This is useful for validating that a value exists as a key within a given array.&lt;/p&gt;

&lt;p&gt;For example, if you have an array of permissions and you want to ensure the &lt;code&gt;selected_permission&lt;/code&gt; is one of the valid keys:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;request-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;
    &lt;span class="s2"&gt;'permissions'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;'required'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'array'&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
    &lt;span class="s2"&gt;'selected_permission'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;'required'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'string'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'in_array_keys:permissions'&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
&lt;span class="o"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a more concise way to handle this common validation scenario.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. &lt;code&gt;isset()&lt;/code&gt; with Multiple Parameters
&lt;/h3&gt;

&lt;p&gt;Did you know that PHP's &lt;code&gt;isset()&lt;/code&gt; function can accept multiple parameters? This is a handy and often overlooked feature.&lt;/p&gt;

&lt;p&gt;The function will return &lt;code&gt;true&lt;/code&gt; only if all the provided variables are set and not &lt;code&gt;null&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;isset&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;data&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;meta&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;items&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;This&lt;/span&gt; &lt;span class="err"&gt;code&lt;/span&gt; &lt;span class="err"&gt;will&lt;/span&gt; &lt;span class="err"&gt;only&lt;/span&gt; &lt;span class="err"&gt;execute&lt;/span&gt; &lt;span class="err"&gt;if&lt;/span&gt; &lt;span class="err"&gt;all&lt;/span&gt; &lt;span class="err"&gt;three&lt;/span&gt; &lt;span class="err"&gt;variables&lt;/span&gt; &lt;span class="err"&gt;are&lt;/span&gt; &lt;span class="err"&gt;set.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can simplify your conditional checks and make your code more readable by avoiding nested &lt;code&gt;if&lt;/code&gt; statements or multiple &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; checks.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Exception Methods for Granular Error Messages
&lt;/h3&gt;

&lt;p&gt;Sometimes, a single exception class can handle multiple types of errors. You can define different methods within a custom exception class to provide specific error messages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;In&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;custom&lt;/span&gt; &lt;span class="nt"&gt;exception&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt;
&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;PaymentException&lt;/span&gt; &lt;span class="nt"&gt;extends&lt;/span&gt; &lt;span class="nt"&gt;Exception&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;static&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;cardExpired():&lt;/span&gt; &lt;span class="err"&gt;self&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;return&lt;/span&gt; &lt;span class="err"&gt;new&lt;/span&gt; &lt;span class="err"&gt;self('The&lt;/span&gt; &lt;span class="err"&gt;credit&lt;/span&gt; &lt;span class="err"&gt;card&lt;/span&gt; &lt;span class="err"&gt;has&lt;/span&gt; &lt;span class="err"&gt;expired.');&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nt"&gt;public&lt;/span&gt; &lt;span class="nt"&gt;static&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;insufficientFunds&lt;/span&gt;&lt;span class="o"&gt;():&lt;/span&gt; &lt;span class="nt"&gt;self&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;return&lt;/span&gt; &lt;span class="err"&gt;new&lt;/span&gt; &lt;span class="err"&gt;self('There&lt;/span&gt; &lt;span class="err"&gt;are&lt;/span&gt; &lt;span class="err"&gt;insufficient&lt;/span&gt; &lt;span class="err"&gt;funds&lt;/span&gt; &lt;span class="err"&gt;to&lt;/span&gt; &lt;span class="err"&gt;complete&lt;/span&gt; &lt;span class="err"&gt;the&lt;/span&gt; &lt;span class="err"&gt;payment.');&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;In&lt;/span&gt; &lt;span class="nt"&gt;your&lt;/span&gt; &lt;span class="nt"&gt;controller&lt;/span&gt;
&lt;span class="nt"&gt;throw&lt;/span&gt; &lt;span class="nt"&gt;PaymentException&lt;/span&gt;&lt;span class="nd"&gt;::cardExpired&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While you can also create separate exception classes for each scenario, this method allows you to group related exceptions under a single class.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. The&amp;nbsp;&lt;code&gt;whereBetween()&lt;/code&gt; Query Builder Shortcut
&lt;/h3&gt;

&lt;p&gt;Laravel &lt;code&gt;12.21&lt;/code&gt; introduced a small but powerful syntactic sugar to the query builder: &lt;code&gt;whereBetween()&lt;/code&gt;. This method simplifies queries where you need to check if a value falls between two other values.&lt;/p&gt;

&lt;p&gt;Instead of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;query-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;where&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'value'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'&amp;gt;='&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;from&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;where&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'value'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'&amp;lt;='&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;to&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can write this:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;query-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;whereBetween&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'value'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;from&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;to&lt;/span&gt;&lt;span class="o"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;This makes your queries more concise and easier to read.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. &lt;code&gt;Str::password()&lt;/code&gt; for Secure Passwords&amp;nbsp;
&lt;/h3&gt;

&lt;p&gt;When you need to generate a secure random password, the &lt;code&gt;Str::password()&lt;/code&gt; helper is the ideal tool. It gives you fine-grained control over the generated string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Str&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nt"&gt;Str&lt;/span&gt;&lt;span class="nd"&gt;::password&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="nt"&gt;length&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="err"&gt;12&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="nt"&gt;letters&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="nt"&gt;numbers&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="nt"&gt;symbols&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;true&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="nt"&gt;spaces&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;false&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can specify the length and whether the password should include letters, numbers, symbols, or spaces. This is a much better alternative to &lt;code&gt;Str::random()&lt;/code&gt; for generating passwords.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Database-Backed Notifications
&lt;/h3&gt;

&lt;p&gt;Laravel's notification system is incredibly flexible. While it's commonly used for emails, you can also store notifications directly in your database. This is perfect for creating in-app notification systems (like the alerts you see on many websites).&lt;/p&gt;

&lt;p&gt;You can use methods like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$user-&amp;gt;unreadNotifications()&lt;/code&gt;: Retrieves all unread notifications.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$notification-&amp;gt;markAsRead()&lt;/code&gt;: Marks a specific notification as read.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;$user-&amp;gt;markAsRead()&lt;/code&gt;: Marks all notifications for a user as read.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can even send a notification to multiple channels at once (e.g., both email and the database) by specifying them in your notification class's &lt;code&gt;via()&lt;/code&gt; method.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Nested Database Transactions
&lt;/h3&gt;

&lt;p&gt;Laravel allows you to nest database transactions, and the framework will scope them to the outermost transaction. This means if a nested transaction fails, the entire chain of transactions will be rolled back.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;DB&lt;/span&gt;&lt;span class="nd"&gt;::transaction&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;This&lt;/span&gt; &lt;span class="err"&gt;is&lt;/span&gt; &lt;span class="err"&gt;the&lt;/span&gt; &lt;span class="err"&gt;outer&lt;/span&gt; &lt;span class="err"&gt;transaction&lt;/span&gt;
    &lt;span class="err"&gt;$user&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="py"&gt;User&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;

    &lt;span class="py"&gt;DB&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;//&lt;/span&gt; &lt;span class="n"&gt;This&lt;/span&gt; &lt;span class="n"&gt;is&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;nested&lt;/span&gt; &lt;span class="n"&gt;transaction&lt;/span&gt;
        &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;user-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;

        &lt;span class="err"&gt;if&lt;/span&gt; &lt;span class="err"&gt;(!$profile)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;Throwing&lt;/span&gt; &lt;span class="err"&gt;an&lt;/span&gt; &lt;span class="err"&gt;exception&lt;/span&gt; &lt;span class="err"&gt;here&lt;/span&gt; &lt;span class="err"&gt;will&lt;/span&gt; &lt;span class="err"&gt;roll&lt;/span&gt; &lt;span class="err"&gt;back&lt;/span&gt; &lt;span class="err"&gt;both&lt;/span&gt; &lt;span class="err"&gt;transactions&lt;/span&gt;
            &lt;span class="err"&gt;throw&lt;/span&gt; &lt;span class="err"&gt;new&lt;/span&gt; &lt;span class="err"&gt;Exception('Profile&lt;/span&gt; &lt;span class="err"&gt;creation&lt;/span&gt; &lt;span class="err"&gt;failed.');&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be useful if you have reusable actions that need to operate within their own transaction but also need to be called from a larger, overarching transaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  11. Custom Validation Message Attributes
&lt;/h3&gt;

&lt;p&gt;The default Laravel validation messages often include the word "field," as in "The name field is required." You can make these messages more user-friendly by simply removing the word "field" from the validation file.&lt;/p&gt;

&lt;p&gt;Simply update your &lt;code&gt;lang/en/validation.php&lt;/code&gt; file to change:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="s2"&gt;'required'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'The :attribute field is required.'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="s2"&gt;'required'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'The :attribute is required.'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, your error message will read "The name is required," which is clearer and less technical for the end-user.&lt;/p&gt;

&lt;h3&gt;
  
  
  12. Grouping Tests in Pest with &lt;code&gt;describe&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If you use the Pest testing framework, you can use the &lt;code&gt;describe()&lt;/code&gt; function to group related tests. This is not only for organization but also for sharing setup and teardown logic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;describe&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'User creation'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;beforeEach(function&lt;/span&gt; &lt;span class="err"&gt;()&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;This&lt;/span&gt; &lt;span class="err"&gt;runs&lt;/span&gt; &lt;span class="err"&gt;before&lt;/span&gt; &lt;span class="err"&gt;each&lt;/span&gt; &lt;span class="err"&gt;test&lt;/span&gt; &lt;span class="err"&gt;in&lt;/span&gt; &lt;span class="err"&gt;this&lt;/span&gt; &lt;span class="err"&gt;group&lt;/span&gt;
        &lt;span class="err"&gt;$this-&amp;gt;actingAs(&lt;/span&gt;&lt;span class="py"&gt;User&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nt"&gt;it&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'can create a user'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;...&lt;/span&gt; &lt;span class="err"&gt;test&lt;/span&gt; &lt;span class="err"&gt;logic&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nt"&gt;it&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'validates user data'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;...&lt;/span&gt; &lt;span class="err"&gt;test&lt;/span&gt; &lt;span class="err"&gt;logic&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This feature, introduced in Pest &lt;code&gt;v0.2.9&lt;/code&gt;, makes your tests more organized and maintainable, especially for complex features.&lt;/p&gt;

&lt;p&gt;Have you learned anything new from this list? Which of these tips will you be adding to your own Laravel projects? Let us know in the comments!&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;🚀 &lt;em&gt;Want more powerful Laravel tips like this?&lt;/em&gt;&lt;br&gt;
Join my newsletter and stay ahead with exclusive insights 👉 &lt;a href="https://laraveldailytips.com" rel="noopener noreferrer"&gt;&lt;strong&gt;LaravelDailyTips.com&lt;/strong&gt;&lt;/a&gt; 💡&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>laraveltips</category>
    </item>
    <item>
      <title>Livewire 4: Faster, Simpler, and More Powerful!</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Mon, 04 Aug 2025 15:49:41 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/livewire-4-faster-simpler-and-more-powerful-bo5</link>
      <guid>https://dev.to/laravel_dailytips/livewire-4-faster-simpler-and-more-powerful-bo5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Livewire 4&lt;/strong&gt; has officially launched, and it’s packed with exciting features aimed at making your Laravel development faster, easier, and more organized. Caleb Porzio unveiled these updates at &lt;strong&gt;Laracon US 2025&lt;/strong&gt;, and while we strongly recommend watching his talk, here’s a super simple breakdown of everything you need to know, with real-life examples!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Single-File Components (SFC) Are the New Default
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Changed?
&lt;/h3&gt;

&lt;p&gt;When you now run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;php&lt;/span&gt; &lt;span class="nt"&gt;artisan&lt;/span&gt; &lt;span class="nt"&gt;make&lt;/span&gt;&lt;span class="nd"&gt;:livewire&lt;/span&gt; &lt;span class="nt"&gt;Counter&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Livewire 4 will create a &lt;strong&gt;single file&lt;/strong&gt; that contains all the logic (PHP), HTML, and JavaScript for your component.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;counter&lt;/span&gt;&lt;span class="nc"&gt;.blade.php&lt;/span&gt;  
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="nt"&gt;wire&lt;/span&gt;&lt;span class="nd"&gt;:click&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"increment"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;+&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$count&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;JS&lt;/span&gt; &lt;span class="nt"&gt;logic&lt;/span&gt; &lt;span class="nt"&gt;here&lt;/span&gt;  
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;script&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  

&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;  
&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;Counter&lt;/span&gt; &lt;span class="nt"&gt;extends&lt;/span&gt; &lt;span class="nt"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;$count&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;0;&lt;/span&gt;  
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;increment()&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$this-&amp;gt;count++;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="err"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;?&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why It’s Awesome:
&lt;/h3&gt;

&lt;p&gt;You don’t have to switch between three different files. Everything is in one file—clean and compact!&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example:
&lt;/h3&gt;

&lt;p&gt;It’s like using a single notepad to jot down a recipe: ingredients, steps, and cooking notes, all in one place. Easy to manage and update!&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Multi-File Components (MFC) When You Need More Structure
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Changed?
&lt;/h3&gt;

&lt;p&gt;If your component starts growing and you want to separate things for better organization, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;php&lt;/span&gt; &lt;span class="nt"&gt;artisan&lt;/span&gt; &lt;span class="nt"&gt;make&lt;/span&gt;&lt;span class="nd"&gt;:livewire&lt;/span&gt; &lt;span class="nt"&gt;profile&lt;/span&gt; &lt;span class="nt"&gt;--mfc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will generate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Profile.php&lt;/code&gt;&amp;nbsp;(Logic)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Profile.blade.php&lt;/code&gt;&amp;nbsp;(View)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Profile.js&lt;/code&gt; (JavaScript)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;profile.test.php&lt;/code&gt; (Test file)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All in the same folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why It’s Awesome:
&lt;/h3&gt;

&lt;p&gt;Easier to maintain big components. You can focus on just the HTML or just the JS, without distractions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example:
&lt;/h3&gt;

&lt;p&gt;Imagine your kitchen tools, cutlery in one drawer, spices on one shelf, and cooking pots in a cabinet. Everything is separate but close together.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Livewire::visit(): Browser Testing Like a Human
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Changed?
&lt;/h3&gt;

&lt;p&gt;Pest 4 introduces&amp;nbsp;&lt;code&gt;Livewire::visit()&lt;/code&gt;, letting you test Livewire components&amp;nbsp;&lt;strong&gt;in a real browser&lt;/strong&gt; (thanks to Playwright).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;test&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'counter increments'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="py"&gt;Livewire&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;visit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;assertSee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'button'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;assertSee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why It’s Awesome:
&lt;/h3&gt;

&lt;p&gt;You can test your Livewire components in the browser just like an actual user would click around. Great for catching real-world issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example:
&lt;/h3&gt;

&lt;p&gt;Think of this as practicing a speech in front of a mirror. You get to see how it &lt;strong&gt;actually performs&lt;/strong&gt;, not just how it looks on paper.&lt;/p&gt;

&lt;h2&gt;
  
  
  4.&amp;nbsp;⚡Blaze – Supercharged Rendering Performance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Changed?
&lt;/h3&gt;

&lt;p&gt;Livewire 4 introduces &lt;strong&gt;Blaze&lt;/strong&gt;, a performance engine under the hood that makes rendering much faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why It’s Awesome:
&lt;/h3&gt;

&lt;p&gt;Pages load quicker. Updates happen in a snap. Less waiting means a better user experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example:
&lt;/h3&gt;

&lt;p&gt;It’s like switching from a bicycle to an electric scooter. You’re still doing the same thing (going from A to B), but much faster!&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Component Slots – Flexible and Reusable
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Changed?
&lt;/h3&gt;

&lt;p&gt;Livewire components now support &lt;strong&gt;slots&lt;/strong&gt; with syntax like &lt;code&gt;{{ $slot }}&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;alert&lt;/span&gt;&lt;span class="nc"&gt;.blade.php&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="nt"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"alert"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$slot&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;Content&lt;/span&gt; &lt;span class="nt"&gt;goes&lt;/span&gt; &lt;span class="nt"&gt;here&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  

&lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;Usage&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;x-alert&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;Success&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nt"&gt;Your&lt;/span&gt; &lt;span class="nt"&gt;order&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;placed&lt;/span&gt;&lt;span class="o"&gt;.&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="o"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;x-alert&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why It’s Awesome:
&lt;/h3&gt;

&lt;p&gt;You can create flexible components and inject content wherever needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example:
&lt;/h3&gt;

&lt;p&gt;Imagine a gift box where you can swap in a birthday card or a wedding card depending on the occasion. Same box, different content!&lt;/p&gt;

&lt;h2&gt;
  
  
  6. &lt;a class="mentioned-user" href="https://dev.to/island"&gt;@island&lt;/a&gt; Directive – Isolate the Heavy Parts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What Changed?
&lt;/h3&gt;

&lt;p&gt;With the &lt;code&gt;@island&lt;/code&gt; directive, you can isolate expensive or slow parts of a component. You can even make it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load lazily (&lt;code&gt;lazy: true&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Refresh (poll) every few seconds (&lt;code&gt;@island(poll: '5s')&lt;/code&gt;)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;Fast&lt;/span&gt; &lt;span class="nt"&gt;part&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;Welcome&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;{&lt;/span&gt; &lt;span class="err"&gt;$user-&amp;gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  

    &lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;Slow&lt;/span&gt; &lt;span class="nt"&gt;part&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;isolated&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="k"&gt;@island&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'5s'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
        &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;livewire&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;order-history&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;  
    &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;endisland&lt;/span&gt;  
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why It’s Awesome:
&lt;/h3&gt;

&lt;p&gt;This boosts performance. Only the “island” loads when needed, not the whole page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example:
&lt;/h3&gt;

&lt;p&gt;Think of this like lazy-loading images on a long article. The full article loads quickly, and images load when they come into view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Placeholder Support While Islands Load
&lt;/h2&gt;

&lt;p&gt;You can even display a &lt;strong&gt;skeleton loader&lt;/strong&gt; or a &lt;strong&gt;loading message&lt;/strong&gt; until the island content is ready:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@island&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;x-loading-spinner&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;endisland&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Real-Life Example:
&lt;/h3&gt;

&lt;p&gt;It’s like showing a "loading…" screen while your food order is being prepared. Keeps the user informed and engaged.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts:
&lt;/h2&gt;

&lt;p&gt;Livewire 4 focuses on &lt;strong&gt;developer experience (DX)&lt;/strong&gt; and &lt;strong&gt;performance&lt;/strong&gt;. Whether you're building small UI components or full dashboards, the new updates will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simplify your workflow&lt;/li&gt;
&lt;li&gt;Improve performance&lt;/li&gt;
&lt;li&gt;Make testing easier&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Next step?&lt;/strong&gt; Watch &lt;a href="https://www.youtube.com/live/GM0glP77tsA?si=JOgUL6ho_-ZQkcRs&amp;amp;t=18740" rel="noopener noreferrer"&gt;Caleb’s talk from Laracon US 2025&lt;/a&gt; and try these features in your next project.&lt;/p&gt;

&lt;p&gt;🚀 &lt;em&gt;Want more powerful Laravel tips like this?&lt;/em&gt;&lt;br&gt;
Join my newsletter and stay ahead with exclusive insights 👉 &lt;a href="https://laraveldailytips.com" rel="noopener noreferrer"&gt;&lt;strong&gt;LaravelDailyTips.com&lt;/strong&gt;&lt;/a&gt; 💡&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>laracon2025</category>
      <category>livewire</category>
    </item>
    <item>
      <title>Laravel 12: Simplifies Soft Delete Routing with New softDeletableResources() Method</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Mon, 04 Aug 2025 15:48:06 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/laravel-12-simplifies-soft-delete-routing-with-new-softdeletableresources-method-26k</link>
      <guid>https://dev.to/laravel_dailytips/laravel-12-simplifies-soft-delete-routing-with-new-softdeletableresources-method-26k</guid>
      <description>&lt;p&gt;Laravel 12.22 introduces a new method called &lt;code&gt;softDeletableResources()&lt;/code&gt; to simplify how developers define routes for models that use soft deletes. Previously, developers had to manually attach &lt;code&gt;-&amp;gt;withTrashed()&lt;/code&gt; to each resource route, which could clutter the route files, especially when dealing with multiple soft-deletable models. This new approach groups those routes into a single, elegant call, improving code clarity and reducing repetition.&lt;/p&gt;

&lt;p&gt;The new method is not only more readable but also aligns with Laravel’s philosophy of clean and expressive syntax. By centralizing soft-deletable routes, it becomes easier to manage and scale your application without losing track of special route configurations. Laravel continues to deliver thoughtful improvements that help developers write cleaner, more maintainable code&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Laravel Routing Gets a Cleanup for Soft Deletes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When working with models that implement &lt;code&gt;SoftDeletes&lt;/code&gt; in Laravel, it’s common to append &lt;code&gt;.withTrashed()&lt;/code&gt; to your resource routes. While this works perfectly, it becomes repetitive and cluttered as the number of soft-deletable models grows.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Old Way:
&lt;/h4&gt;

&lt;p&gt;Here’s how developers traditionally defined soft-deletable resource routes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;Route&lt;/span&gt;&lt;span class="nd"&gt;::resource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'users'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;UserController&lt;/span&gt;&lt;span class="nd"&gt;::class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;withTrashed&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="nt"&gt;Route&lt;/span&gt;&lt;span class="nd"&gt;::resource&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'blogs'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;BlogController&lt;/span&gt;&lt;span class="nd"&gt;::class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;withTrashed&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While functional, repeating &lt;code&gt;-&amp;gt;withTrashed()&lt;/code&gt; across routes clutters your routes file and breaks DRY principles.&lt;/p&gt;

&lt;h4&gt;
  
  
  What’s New in Laravel 12.22?
&lt;/h4&gt;

&lt;p&gt;Laravel 12.22 introduces a new method to clean up your route definitions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;Route&lt;/span&gt;&lt;span class="nd"&gt;::softDeletableResources&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;
    &lt;span class="s2"&gt;'users'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;UserController&lt;/span&gt;&lt;span class="nd"&gt;::class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'blogs'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;BlogController&lt;/span&gt;&lt;span class="nd"&gt;::class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method allows you to register multiple soft-deletable resource routes in a single, clean call.&lt;/p&gt;

&lt;h3&gt;
  
  
  Benefits of &lt;code&gt;softDeletableResources()&lt;/code&gt;:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;DRY-compliant:&lt;/strong&gt; No more repeated &lt;code&gt;-&amp;gt;withTrashed()&lt;/code&gt; calls.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean structure:&lt;/strong&gt; Routes become more readable and manageable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalable:&lt;/strong&gt; Add or remove soft-deletable resources with ease.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When Should You Use It?
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;softDeletableResources()&lt;/code&gt; when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your models implement &lt;code&gt;Illuminate\Database\Eloquent\SoftDeletes&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You want to expose trashed records via standard resource routes.&lt;/li&gt;
&lt;li&gt;You care about maintaining a tidy and maintainable routes file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;Laravel’s updates consistently aim to reduce boilerplate code. With the &lt;code&gt;softDeletableResources()&lt;/code&gt; addition in 12.22, you can streamline your soft delete routes and focus more on building features instead of writing repetitive route definitions.&lt;/p&gt;

&lt;p&gt;Keep an eye out for more elegant updates like this in future Laravel versions!&lt;/p&gt;

&lt;p&gt;🚀 Want more powerful Laravel tips like this?&lt;br&gt;
Join my newsletter and stay ahead with exclusive insights 👉 &lt;a href="https://laraveldailytips.com" rel="noopener noreferrer"&gt;&lt;strong&gt;LaravelDailyTips.com&lt;/strong&gt;&lt;/a&gt; 💡&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>programming</category>
      <category>softdeletes</category>
      <category>refactoring</category>
    </item>
    <item>
      <title>Boost Laravel Performance with LazyCollection and cursor()</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Mon, 04 Aug 2025 15:46:18 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/boost-laravel-performance-with-lazycollection-and-cursor-be</link>
      <guid>https://dev.to/laravel_dailytips/boost-laravel-performance-with-lazycollection-and-cursor-be</guid>
      <description>&lt;p&gt;Laravel LazyCollection and &lt;code&gt;cursor()&lt;/code&gt; are game-changers for handling large datasets efficiently. Instead of consuming all memory upfront, these tools stream data line-by-line or record-by-record. For instance, using LazyCollection to parse a user activity file or cursor() for high-value order reports ensures your application remains fast and responsive even with massive data.&lt;/p&gt;

&lt;p&gt;These techniques not only improve Laravel performance but also help with tasks like CSV processing, large database queries, and memory optimization in Laravel. Implementing them in your Laravel workflow guarantees scalable and efficient application architecture.&lt;/p&gt;

&lt;p&gt;Managing large datasets in Laravel can be overwhelming, especially when server memory becomes a bottleneck. Laravel's &lt;code&gt;LazyCollection&lt;/code&gt; and &lt;code&gt;cursor()&lt;/code&gt; method offer powerful tools to handle this challenge effectively by reducing memory consumption during data processing.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is LazyCollection?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;LazyCollection&lt;/code&gt; is a memory-friendly feature in Laravel that reads data only when it’s needed, instead of loading everything into memory at once. This lazy-loading behavior is perfect for working with big files, streaming responses, or looping through extensive data.&lt;/p&gt;

&lt;p&gt;Here’s a simple use-case of reading a log file and processing lines containing errors:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;LazyCollection&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;LazyCollection&lt;/span&gt;&lt;span class="nd"&gt;::make&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;$handle&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;fopen(storage_path('logs/system.log'),&lt;/span&gt; &lt;span class="err"&gt;'r');&lt;/span&gt;
    &lt;span class="err"&gt;while&lt;/span&gt; &lt;span class="err"&gt;(($line&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;fgets($handle))&lt;/span&gt; &lt;span class="err"&gt;!==&lt;/span&gt; &lt;span class="err"&gt;false)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;yield&lt;/span&gt; &lt;span class="err"&gt;$line;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;filter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;line&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;str_contains&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;line&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'ERROR'&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;each&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;errorLine&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;Handle&lt;/span&gt; &lt;span class="err"&gt;each&lt;/span&gt; &lt;span class="err"&gt;error&lt;/span&gt; &lt;span class="err"&gt;line&lt;/span&gt; &lt;span class="err"&gt;(e.g.,&lt;/span&gt; &lt;span class="err"&gt;store&lt;/span&gt; &lt;span class="err"&gt;or&lt;/span&gt; &lt;span class="err"&gt;alert)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows Laravel to process a huge log file without crashing due to memory overload.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-world Application: Importing User Activity
&lt;/h3&gt;

&lt;p&gt;Suppose you’re importing user activity from a text file for analysis. &lt;code&gt;LazyCollection&lt;/code&gt; can help transform and insert data in chunks without overloading the system.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;UserActivity&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;LazyCollection&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;UserActivityImporter&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;import(string&lt;/span&gt; &lt;span class="err"&gt;$filepath)&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="py"&gt;LazyCollection&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;filepath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'r'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="err"&gt;while&lt;/span&gt; &lt;span class="err"&gt;(($line&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;fgets($file))&lt;/span&gt; &lt;span class="err"&gt;!==&lt;/span&gt; &lt;span class="err"&gt;false)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
                &lt;span class="err"&gt;yield&lt;/span&gt; &lt;span class="err"&gt;explode('|',&lt;/span&gt; &lt;span class="err"&gt;trim($line));&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
            &lt;span class="s2"&gt;'user_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
            &lt;span class="s2"&gt;'action'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
            &lt;span class="s2"&gt;'timestamp'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="err"&gt;2&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
        &lt;span class="o"&gt;])&lt;/span&gt;
        &lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;chunk&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;300&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;each&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;chunk&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;UserActivity&lt;/span&gt;&lt;span class="nd"&gt;::insert&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;chunk-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;all&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using cursor() for Large DB Queries
&lt;/h3&gt;

&lt;p&gt;For database-heavy applications, Laravel’s &lt;code&gt;cursor()&lt;/code&gt; method retrieves results lazily using generators, this helps process millions of rows without memory issues.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;OrderAnalyzer&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;analyze()&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="py"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;order-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="err"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;5000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;this-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;notifyHighSpender&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nt"&gt;protected&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;notifyHighSpender&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;order&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;Send&lt;/span&gt; &lt;span class="err"&gt;notification&lt;/span&gt; &lt;span class="err"&gt;or&lt;/span&gt; &lt;span class="err"&gt;flag&lt;/span&gt; &lt;span class="err"&gt;order&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This technique avoids loading all orders into memory and allows on-the-fly processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thoughts:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Leveraging Laravel's&amp;nbsp;&lt;code&gt;LazyCollection&lt;/code&gt; and &lt;code&gt;cursor()&lt;/code&gt; transforms how you handle large datasets, turning memory-heavy tasks into streamlined, efficient processes. Whether you're importing logs, processing user activity, or analyzing millions of records, these tools help keep performance high and memory usage low. Start using them today to build scalable, high-performing Laravel applications&lt;/p&gt;

&lt;p&gt;🚀 Want more powerful Laravel tips like this?&lt;br&gt;
Join my newsletter and stay ahead with exclusive insights 👉 &lt;a href="https://laraveldailytips.com" rel="noopener noreferrer"&gt;&lt;strong&gt;LaravelDailyTips.com&lt;/strong&gt;&lt;/a&gt; 💡&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>lazycollection</category>
      <category>laravelcursor</category>
      <category>security</category>
    </item>
    <item>
      <title>Top Git Interview Questions and Answers for 2025</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Fri, 01 Aug 2025 14:24:12 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/top-git-interview-questions-and-answers-for-2025-1o7a</link>
      <guid>https://dev.to/laravel_dailytips/top-git-interview-questions-and-answers-for-2025-1o7a</guid>
      <description>&lt;p&gt;In the fast-evolving world of development, &lt;strong&gt;Git&lt;/strong&gt; remains the backbone of version control systems. Whether you're a beginner trying to land your first job or a senior developer preparing for a tech lead role, understanding Git is crucial.&lt;/p&gt;

&lt;p&gt;In this blog, you'll find a curated list of &lt;strong&gt;Git interview questions and answers for 2025&lt;/strong&gt;, starting from basics and moving to advanced topics. These questions are frequently asked in technical interviews for roles like frontend, backend, DevOps, and full-stack developers.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;What is Git?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Git is a distributed version control system used to track changes in source code. It allows multiple developers to work collaboratively while maintaining a history of changes.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. &lt;strong&gt;What is the difference between Git and GitHub?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Git is a version control system, whereas GitHub is a cloud-based hosting service for Git repositories. Git is the tool; GitHub is the platform.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. &lt;strong&gt;What is a repository in Git?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
A Git repository (repo) is a storage space where your project and its version history are stored.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. &lt;strong&gt;What are the basic Git commands every developer should know?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git init&lt;/code&gt;: Initialize a repository&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git add&lt;/code&gt;: Add files to staging&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git commit&lt;/code&gt;: Save changes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git status&lt;/code&gt;: Show current changes&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git push&lt;/code&gt;: Push to remote&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git pull&lt;/code&gt;: Pull from remote&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git clone&lt;/code&gt;: Clone a repository&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  5.&amp;nbsp;&lt;strong&gt;What is the difference between &lt;/strong&gt;&lt;code&gt;git pull&lt;/code&gt;&lt;strong&gt; and &lt;/strong&gt;&lt;code&gt;git fetch&lt;/code&gt;&lt;strong&gt;?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git fetch&lt;/code&gt; downloads changes but doesn't merge them automatically.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git pull&lt;/code&gt; downloads and merges the changes from the remote to your local branch.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  6. &lt;strong&gt;What is the difference between &lt;/strong&gt;&lt;code&gt;git merge&lt;/code&gt;&lt;strong&gt; and &lt;/strong&gt;&lt;code&gt;git rebase&lt;/code&gt;&lt;strong&gt;?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git merge&lt;/code&gt;: Combines two branches and creates a new merge commit.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git rebase&lt;/code&gt;: Moves or re-applies commits on top of another base branch, creating a cleaner history.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  7. &lt;strong&gt;Explain the difference between &lt;/strong&gt;&lt;code&gt;git reset&lt;/code&gt;&lt;strong&gt; and &lt;/strong&gt;&lt;code&gt;git revert&lt;/code&gt;&lt;strong&gt;.&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git reset&lt;/code&gt;: Removes commits from history. It can be dangerous for shared branches.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git revert&lt;/code&gt;: Creates a new commit that undoes changes from a previous commit. It is safer for public branches.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  8. &lt;strong&gt;What is the use of &lt;/strong&gt;&lt;code&gt;.gitignore&lt;/code&gt;&lt;strong&gt; file?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
It tells Git which files or directories to ignore when committing code, such as environment files, logs, or &lt;code&gt;node_modules&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  9.&amp;nbsp;&lt;strong&gt;How do you resolve merge conflicts in Git?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Manually edit the conflicting files, resolve the changes, mark them as resolved using &lt;code&gt;git add&lt;/code&gt;, then commit.&lt;/p&gt;
&lt;h3&gt;
  
  
  10. &lt;strong&gt;What is a detached HEAD in Git?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
It means Git is not pointing to the latest commit on a branch. This happens when you checkout a commit directly instead of a branch.&lt;/p&gt;
&lt;h3&gt;
  
  
  11.&amp;nbsp;&lt;strong&gt;What’s the difference between &lt;/strong&gt;&lt;code&gt;git checkout&lt;/code&gt;&lt;strong&gt; and &lt;/strong&gt;&lt;code&gt;git switch&lt;/code&gt;&lt;strong&gt;?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git checkout&lt;/code&gt;: Versatile, but used for both switching branches and restoring files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git switch&lt;/code&gt;: Introduced to simplify branch switching; use &lt;code&gt;git switch&lt;/code&gt; for clarity and best practice.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;switch&lt;/span&gt; &lt;span class="nt"&gt;feature-branch&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  12.&amp;nbsp;&lt;strong&gt;When would you use &lt;/strong&gt;&lt;code&gt;git stash pop&lt;/code&gt;&lt;strong&gt; vs &lt;/strong&gt;&lt;code&gt;git stash apply&lt;/code&gt;&lt;strong&gt;?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git stash pop&lt;/code&gt;: Applies the latest stashed changes and removes them from stash history.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git stash apply&lt;/code&gt;: Applies changes but retains them in the stash list for later use.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;apply&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;safer&lt;/span&gt; &lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;pop&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;destructive&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;removes&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  13.&amp;nbsp;&lt;strong&gt;How to revert a specific commit?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;revert&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;commit-hash&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new commit that undoes the changes from the target commit.&lt;/p&gt;

&lt;h3&gt;
  
  
  14.&amp;nbsp;&lt;strong&gt;Difference between soft, mixed, and hard reset in Git?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--soft&lt;/code&gt;: Moves HEAD, keeps staging area and working directory unchanged.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--mixed&lt;/code&gt; (default): Moves HEAD and clears staging area.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--hard&lt;/code&gt;: Moves HEAD and resets staging and working directory.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reset&lt;/span&gt; &lt;span class="nt"&gt;--soft&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="err"&gt;1&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  15.&amp;nbsp;&lt;strong&gt;How to undo local commits that haven’t been pushed yet?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reset&lt;/span&gt; &lt;span class="nt"&gt;--soft&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;n&lt;/code&gt; with the number of commits you want to undo.&lt;/p&gt;

&lt;h3&gt;
  
  
  16.&amp;nbsp;&lt;strong&gt;How to see the commit history in Git?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;log&lt;/span&gt; &lt;span class="nt"&gt;--oneline&lt;/span&gt; &lt;span class="nt"&gt;--graph&lt;/span&gt; &lt;span class="nt"&gt;--all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a visual representation.&lt;/p&gt;

&lt;h3&gt;
  
  
  17.&amp;nbsp;&lt;strong&gt;How to track a remote branch in Git?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt; &lt;span class="nt"&gt;--track&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;branch-name&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  18.&amp;nbsp;&lt;strong&gt;What’s the difference between tracking and non-tracking branches?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;br&gt;
Tracking branches are linked to remote branches and can use &lt;code&gt;git pull&lt;/code&gt; and &lt;code&gt;git push&lt;/code&gt; directly. Non-tracking branches are not associated with any remote.&lt;/p&gt;
&lt;h3&gt;
  
  
  19. &lt;strong&gt;How to clean untracked files in Git?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;clean&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt;  &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;files&lt;/span&gt; &lt;span class="nt"&gt;only&lt;/span&gt; &lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;clean&lt;/span&gt; &lt;span class="nt"&gt;-fd&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;files&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;directories&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  20.&amp;nbsp;&lt;strong&gt;How to squash multiple commits into one?&lt;/strong&gt;
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;rebase&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="nt"&gt;n&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then mark commits as &lt;code&gt;squash&lt;/code&gt; in the interactive editor.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thoughts:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Mastering Git is essential for every developer. Interviewers often assess your problem-solving ability with Git commands during real-world issues like conflict resolution, bad commits, and deployment rollbacks.&lt;/p&gt;

&lt;p&gt;These &lt;strong&gt;Git interview questions and answers for 2025&lt;/strong&gt; will not only help you succeed in interviews but also sharpen your daily development workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  📣&amp;nbsp;&lt;strong&gt;Want More?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;👉 Subscribe to our newsletter for daily Laravel, Git &amp;amp; DevOps insights&lt;/p&gt;

&lt;p&gt;👉 Read next: &lt;a href="https://laraveldailytips.com/git-workflow-strategies-explained-simply" rel="noopener noreferrer"&gt;Git Workflow Strategies – Explained Simply&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Read next: &lt;a href="https://laraveldailytips.com/advanced-git-mastery-second-edition-2025" rel="noopener noreferrer"&gt;Advanced Git Mastery – Second edition 2025&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Read next: &lt;a href="https://laraveldailytips.com/advanced-git-mastery-first-edition-2025" rel="noopener noreferrer"&gt;Advanced Git Mastery – First edition 2025&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Read next: &lt;a href="https://laraveldailytips.com/git-for-intermediate" rel="noopener noreferrer"&gt;Git for Intermediate – Branching, Remotes, Undoing Changes &amp;amp; More&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉 Read next: &lt;a href="https://laraveldailytips.com/git-for-beginners-a-complete-getting-started-guide-with-examples" rel="noopener noreferrer"&gt;Git for Beginners – A Complete Getting Started Guide with Examples&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>interview</category>
    </item>
    <item>
      <title>Advanced Git Mastery – Second edition 2025</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Thu, 31 Jul 2025 15:07:02 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/advanced-git-mastery-second-edition-2025-elp</link>
      <guid>https://dev.to/laravel_dailytips/advanced-git-mastery-second-edition-2025-elp</guid>
      <description>&lt;p&gt;Git helps you work smarter and safer. With &lt;code&gt;git reflog&lt;/code&gt;, you can recover lost commits, and tools like &lt;code&gt;git clean&lt;/code&gt; and &lt;code&gt;git gc&lt;/code&gt; help you remove extra files and speed up your project. Git aliases let you create shortcuts for commands, saving you time every day. Exploring the &lt;code&gt;.git&lt;/code&gt; folder gives you insight into how Git manages your project internally.&lt;/p&gt;

&lt;p&gt;For team projects, choosing the right Git workflow is key. Git Flow, GitHub Flow, and Trunk-Based Development each support different types of teams and release styles. Writing clear commit messages, squashing fixes, and using pull requests are all best practices that keep your code clean and easy to review.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. &lt;strong&gt;Recovering with Reflog (&lt;/strong&gt;&lt;code&gt;git reflog&lt;/code&gt;&lt;strong&gt;)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is &lt;code&gt;git reflog&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;It tracks &lt;strong&gt;every move of your HEAD&lt;/strong&gt;, even those Git doesn't normally show.&lt;br&gt;
If you accidentally delete a commit, reset a branch, or mess something up, &lt;code&gt;reflog&lt;/code&gt;&lt;strong&gt; can save your day.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Real-World Example: Time Machine for Your Code
&lt;/h3&gt;

&lt;p&gt;Imagine you’re writing a book. Every time you &lt;strong&gt;print a copy (commit)&lt;/strong&gt;, you store it.&lt;/p&gt;

&lt;p&gt;One day, you get confused and:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rip out a few pages&lt;/li&gt;
&lt;li&gt;Or throw away the last version&lt;/li&gt;
&lt;li&gt;Or accidentally go back to an older version&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You think: “Oh no! I lost my best work!”&lt;/p&gt;
&lt;h3&gt;
  
  
  That’s where&amp;nbsp;&lt;code&gt;git reflog&lt;/code&gt; comes in!
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reflog&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;It shows a history like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;a1b2c3d&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;reset&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;moving&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;previous&lt;/span&gt; &lt;span class="nt"&gt;version&lt;/span&gt;
&lt;span class="nt"&gt;f4e5f6g&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;Add&lt;/span&gt; &lt;span class="nt"&gt;contact&lt;/span&gt; &lt;span class="nt"&gt;form&lt;/span&gt;
&lt;span class="nt"&gt;c7d8e9h&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;Fix&lt;/span&gt; &lt;span class="nt"&gt;login&lt;/span&gt; &lt;span class="nt"&gt;issue&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each line = a step you took (checkout, commit, reset, merge, etc.)&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Recover
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. See the history:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reflog&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Choose the commit you want to go back to (copy the hash)
&lt;/h4&gt;

&lt;h4&gt;
  
  
  3. Restore it:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Just look at the old state:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;commit-id&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Or move your branch back completely:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reset&lt;/span&gt; &lt;span class="nt"&gt;--hard&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;commit-id&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Be careful: &lt;code&gt;--hard&lt;/code&gt; will &lt;strong&gt;overwrite&lt;/strong&gt; current working files.&lt;/p&gt;

&lt;h3&gt;
  
  
  When to Use &lt;code&gt;git reflog&lt;/code&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You did &lt;code&gt;git reset --hard&lt;/code&gt; and lost work&lt;/li&gt;
&lt;li&gt;You force-pushed or deleted a branch accidentally&lt;/li&gt;
&lt;li&gt;You want to go back to &lt;strong&gt;any previous state&lt;/strong&gt;, even if it’s not in &lt;code&gt;git log&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sooooo, Now you better understand the magic of &lt;strong&gt;GIT&lt;/strong&gt; and &lt;strong&gt;reflog&lt;/strong&gt; 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  10. &lt;strong&gt;Cleaning Up with &lt;/strong&gt;&lt;code&gt;git clean&lt;/code&gt;&lt;strong&gt;, &lt;/strong&gt;&lt;code&gt;git gc&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. &lt;/strong&gt;&lt;code&gt;git clean&lt;/code&gt;&lt;strong&gt; – Remove Untracked Junk&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you're working on a project, sometimes you create &lt;strong&gt;extra files or folders&lt;/strong&gt; that aren’t tracked by Git, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Temporary files (&lt;code&gt;.log&lt;/code&gt;, &lt;code&gt;.tmp&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;li&gt;Build output (e.g., &lt;code&gt;dist/&lt;/code&gt;, &lt;code&gt;node_modules/&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;li&gt;Mistakenly added files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These don’t show up in &lt;code&gt;git status&lt;/code&gt; as &lt;strong&gt;staged or committed&lt;/strong&gt;, but they &lt;strong&gt;clutter your folder&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-World Analogy:
&lt;/h3&gt;

&lt;p&gt;Imagine your desk has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Files you've already filed in drawers (committed files)&lt;/li&gt;
&lt;li&gt;Files you’ve decided to file next (staged files)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sticky notes, wrappers, and paper scraps&lt;/strong&gt; all over (untracked files)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You use &lt;code&gt;git clean&lt;/code&gt; to &lt;strong&gt;throw away&lt;/strong&gt; those scraps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commands:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;clean&lt;/span&gt; &lt;span class="nt"&gt;-f&lt;/span&gt;     &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Clean&lt;/span&gt; &lt;span class="nt"&gt;untracked&lt;/span&gt; &lt;span class="nt"&gt;files&lt;/span&gt; &lt;span class="nt"&gt;only&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;clean&lt;/span&gt; &lt;span class="nt"&gt;-fd&lt;/span&gt;    &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Clean&lt;/span&gt; &lt;span class="nt"&gt;untracked&lt;/span&gt; &lt;span class="nt"&gt;files&lt;/span&gt; &lt;span class="nt"&gt;AND&lt;/span&gt; &lt;span class="nt"&gt;folders&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Be careful&lt;/strong&gt; – this deletes files permanently.&lt;/p&gt;

&lt;p&gt;To &lt;strong&gt;preview before deleting&lt;/strong&gt;, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;clean&lt;/span&gt; &lt;span class="nt"&gt;-n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;2. &lt;/strong&gt;&lt;code&gt;git gc&lt;/code&gt;&lt;strong&gt; – Optimize Your Repository&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Over time, Git stores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deleted commits&lt;/li&gt;
&lt;li&gt;Unreachable branches&lt;/li&gt;
&lt;li&gt;Old pack files&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These pile up and make your repo heavier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Think of It Like:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Defragmenting your hard drive&lt;/strong&gt; or &lt;strong&gt;cleaning up cache&lt;/strong&gt;, it keeps things &lt;strong&gt;fast and tidy&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Command:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;gc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This stands for &lt;strong&gt;Garbage Collection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compress files&lt;/li&gt;
&lt;li&gt;Remove unnecessary objects&lt;/li&gt;
&lt;li&gt;Optimize Git's internal storage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  11. &lt;strong&gt;Git Aliases to Boost Productivity&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Speed up typing with aliases.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;config&lt;/span&gt; &lt;span class="nt"&gt;--global&lt;/span&gt; &lt;span class="nt"&gt;alias&lt;/span&gt;&lt;span class="nc"&gt;.st&lt;/span&gt; &lt;span class="nt"&gt;status&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;config&lt;/span&gt; &lt;span class="nt"&gt;--global&lt;/span&gt; &lt;span class="nt"&gt;alias&lt;/span&gt;&lt;span class="nc"&gt;.co&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;st&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;co&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save hours over time!&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Exploring the&amp;nbsp;&lt;code&gt;.git&lt;/code&gt; Folder – The Brain of Your Git Repo
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is&amp;nbsp;&lt;code&gt;.git&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;When you run &lt;code&gt;git init&lt;/code&gt;, Git creates a hidden folder called &lt;code&gt;.git&lt;/code&gt; inside your project.&lt;/p&gt;

&lt;p&gt;This folder is like the &lt;strong&gt;brain and memory&lt;/strong&gt; of your Git project.&lt;br&gt;
It contains &lt;strong&gt;everything Git needs to track changes&lt;/strong&gt;, branches, commits, and settings.&lt;/p&gt;
&lt;h3&gt;
  
  
  Real-World Analogy:
&lt;/h3&gt;

&lt;p&gt;Think of your Git project like a &lt;strong&gt;house&lt;/strong&gt;, and the &lt;code&gt;.git&lt;/code&gt; folder is the &lt;strong&gt;control room&lt;/strong&gt; in the basement.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You don’t see it during daily use&lt;/li&gt;
&lt;li&gt;But it runs everything behind the scenes&lt;/li&gt;
&lt;li&gt;If you delete it, you lose &lt;strong&gt;all history&lt;/strong&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Let’s Explore It
&lt;/h3&gt;

&lt;p&gt;Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;ls&lt;/span&gt; &lt;span class="nc"&gt;.git&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;HEAD&lt;/span&gt;
&lt;span class="nt"&gt;config&lt;/span&gt;
&lt;span class="nt"&gt;objects&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="nt"&gt;refs&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="nt"&gt;logs&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="nt"&gt;index&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Important Parts Explained:
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Folder/File&lt;/th&gt;
&lt;th&gt;What It Does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;objects/&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Stores &lt;strong&gt;all Git data&lt;/strong&gt; (commits, files, trees)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;refs/&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Contains &lt;strong&gt;branches and tags&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;HEAD&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Points to your current branch&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;config&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Local Git &lt;strong&gt;settings&lt;/strong&gt; for this repo&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;logs/&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Stores &lt;strong&gt;reflog history&lt;/strong&gt; – all HEAD movements&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;index&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The &lt;strong&gt;staging area&lt;/strong&gt; snapshot&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Tip:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Do NOT edit files in &lt;/strong&gt;&lt;code&gt;.git&lt;/code&gt;&lt;strong&gt; manually&lt;/strong&gt;, unless:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You really know what you're doing&lt;/li&gt;
&lt;li&gt;You're debugging or recovering a repo&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One wrong change can corrupt your Git repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  14. &lt;strong&gt;Best Practices for Teams&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Writing Good Commit Messages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Start with a capital letter&lt;/li&gt;
&lt;li&gt;Keep the first line under 50 characters&lt;/li&gt;
&lt;li&gt;Describe &lt;em&gt;what&lt;/em&gt; and &lt;em&gt;why&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"Fix login redirect on mobile"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reviewing and Squashing Commits
&lt;/h3&gt;

&lt;p&gt;Use Pull Requests to review code. Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;rebase&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to squash “fix” commits before merging.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling Large Files
&lt;/h3&gt;

&lt;p&gt;Git isn't built for binary files or large assets. Use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;lfs&lt;/span&gt; &lt;span class="nt"&gt;install&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;lfs&lt;/span&gt; &lt;span class="nt"&gt;track&lt;/span&gt; &lt;span class="s1"&gt;"*.psd"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Git LFS (Large File Storage) stores them efficiently outside your repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Git is more than just version control, it's a toolbox for collaboration, automation, and code safety. Mastering these advanced tools can drastically improve your efficiency, team collaboration, and confidence during critical development tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Step:&lt;/strong&gt;&lt;br&gt;
Missed the basics? &lt;a href="https://laraveldailytips.com/git-for-beginners-a-complete-getting-started-guide-with-examples" rel="noopener noreferrer"&gt;Git for Beginners&lt;/a&gt;&lt;br&gt;
Or continue from intermediate level: &lt;a href="https://laraveldailytips.com/git-for-intermediate" rel="noopener noreferrer"&gt;Git Intermediate Guide&lt;/a&gt;&lt;br&gt;
Or continue from advanced level Part 1: &amp;nbsp;&lt;a href="https://laraveldailytips.com/advanced-git-mastery-first-edition-2025" rel="noopener noreferrer"&gt;Git Advanced Guide Part 1&lt;/a&gt;&lt;br&gt;
Or continue from advanced level Part 2: &amp;nbsp;&lt;a href="https://laraveldailytips.com/advanced-git-mastery-second-edition-2025" rel="noopener noreferrer"&gt;Git Advanced Guide Part 2&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advanced Git Mastery – First edition 2025</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Thu, 31 Jul 2025 15:02:51 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/advanced-git-mastery-first-edition-2025-igm</link>
      <guid>https://dev.to/laravel_dailytips/advanced-git-mastery-first-edition-2025-igm</guid>
      <description>&lt;p&gt;&lt;strong&gt;Once you're comfortable with the basics and intermediate Git concepts, it's time to unlock Git's full potential. This guide is packed with expert-level features, clean examples, and real-world workflows that will supercharge your version control skills and team collaboration.&lt;/strong&gt;&lt;br&gt;
&amp;nbsp;&lt;/p&gt;
&lt;h2&gt;
  
  
  1. &lt;strong&gt;Rebase vs Merge – What’s the Difference?&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Merge&lt;/strong&gt; combines histories by creating a new "merge commit."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rebase&lt;/strong&gt; rewrites history by moving your branch on top of another.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Merge&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt; &lt;span class="nt"&gt;feature&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;merge&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Rebase&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt; &lt;span class="nt"&gt;feature&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;rebase&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Use Rebase&lt;/strong&gt; for cleaner, linear history.&lt;br&gt;
&lt;strong&gt;Use Merge&lt;/strong&gt; to preserve the actual branch history.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Simple Example:&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine you’re building a tower of blocks (&lt;code&gt;main&lt;/code&gt;&amp;nbsp;branch).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Merge&lt;/strong&gt;&amp;nbsp;= You add a new block on top that says "merged feature."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rebase&lt;/strong&gt;&amp;nbsp;= You take your blocks and stack them neatly on top of the latest&amp;nbsp;&lt;code&gt;main&lt;/code&gt;&amp;nbsp;tower,&amp;nbsp;&lt;strong&gt;as if you built them there from the start&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. &lt;strong&gt;Interactive Rebase (&lt;/strong&gt;&lt;code&gt;git rebase -i&lt;/code&gt;&lt;strong&gt;)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This lets you clean up commits &lt;strong&gt;before merging&lt;/strong&gt; into the main branch.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;rebase&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="err"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;pick&lt;/span&gt; &lt;span class="err"&gt;123&lt;/span&gt;&lt;span class="nt"&gt;abc&lt;/span&gt; &lt;span class="nt"&gt;Add&lt;/span&gt; &lt;span class="nt"&gt;login&lt;/span&gt; &lt;span class="nt"&gt;form&lt;/span&gt;
&lt;span class="nt"&gt;pick&lt;/span&gt; &lt;span class="err"&gt;456&lt;/span&gt;&lt;span class="nt"&gt;def&lt;/span&gt; &lt;span class="nt"&gt;Fix&lt;/span&gt; &lt;span class="nt"&gt;typo&lt;/span&gt;
&lt;span class="nt"&gt;pick&lt;/span&gt; &lt;span class="err"&gt;789&lt;/span&gt;&lt;span class="nt"&gt;ghi&lt;/span&gt; &lt;span class="nt"&gt;Add&lt;/span&gt; &lt;span class="nt"&gt;validation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;pick&lt;/span&gt; &lt;span class="err"&gt;123&lt;/span&gt;&lt;span class="nt"&gt;abc&lt;/span&gt; &lt;span class="nt"&gt;Add&lt;/span&gt; &lt;span class="nt"&gt;login&lt;/span&gt; &lt;span class="nt"&gt;form&lt;/span&gt;
&lt;span class="nt"&gt;squash&lt;/span&gt; &lt;span class="err"&gt;456&lt;/span&gt;&lt;span class="nt"&gt;def&lt;/span&gt; &lt;span class="nt"&gt;Fix&lt;/span&gt; &lt;span class="nt"&gt;typo&lt;/span&gt;
&lt;span class="nt"&gt;squash&lt;/span&gt; &lt;span class="err"&gt;789&lt;/span&gt;&lt;span class="nt"&gt;ghi&lt;/span&gt; &lt;span class="nt"&gt;Add&lt;/span&gt; &lt;span class="nt"&gt;validation&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It combines multiple commits into one!&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example: Blocks and Rebase -i
&lt;/h3&gt;

&lt;p&gt;Imagine you’ve built a small tower of &lt;strong&gt;3 blocks&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Add login form&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fix typo&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Add validation&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These blocks are your &lt;strong&gt;commits&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now you realize:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Hmm... all these blocks are part of one feature, the login system. I should combine them into &lt;strong&gt;one clean block&lt;/strong&gt; before adding it to the main tower."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  3. &lt;strong&gt;Cherry-Picking Specific Commits&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;git cherry-pick&lt;/code&gt; lets you &lt;strong&gt;copy a single commit&lt;/strong&gt; from one branch to another, &lt;strong&gt;without merging the entire branch&lt;/strong&gt;.&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-Life Example (Building a House):
&lt;/h2&gt;

&lt;p&gt;Imagine you and your friend are building different rooms of a house:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You're working on the &lt;strong&gt;kitchen branch&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Your friend is working on the &lt;strong&gt;living-room branch&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One day, your friend adds a &lt;strong&gt;beautiful ceiling light&lt;/strong&gt; in their branch (it's one commit).&lt;/p&gt;

&lt;p&gt;You think:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Hey! That ceiling light would look great in my kitchen too!”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Instead of merging the entire &lt;strong&gt;living-room&lt;/strong&gt; changes (which might include unrelated furniture), you just want that &lt;strong&gt;one ceiling light&lt;/strong&gt; commit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You Do:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt; &lt;span class="nt"&gt;kitchen&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;cherry-pick&lt;/span&gt; &lt;span class="nt"&gt;a1b2c3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;a1b2c3&lt;/code&gt; is the &lt;strong&gt;commit ID&lt;/strong&gt; for the ceiling light.&lt;/p&gt;

&lt;p&gt;Now, that exact change is &lt;strong&gt;copied into your kitchen branch&lt;/strong&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  4. &lt;strong&gt;Git Hooks – Automate with Pre-commit and Post-commit&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Git lets you run scripts at specific points via &lt;strong&gt;hooks&lt;/strong&gt;. Example: run tests before commit.&lt;/p&gt;

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

&lt;p&gt;Create a file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;touch&lt;/span&gt; &lt;span class="nc"&gt;.git&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;hooks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;pre-commit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Edit the file and add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="o"&gt;!/&lt;/span&gt;&lt;span class="nt"&gt;bin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;sh&lt;/span&gt;
&lt;span class="nt"&gt;php&lt;/span&gt; &lt;span class="nt"&gt;artisan&lt;/span&gt; &lt;span class="nt"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;This tells Git: "Before every commit, run the test suite."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Make it executable&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;chmod&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nt"&gt;x&lt;/span&gt; &lt;span class="nc"&gt;.git&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;hooks&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;pre-commit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now What Happens?&lt;/p&gt;

&lt;p&gt;Every time you run:&lt;/p&gt;

&lt;p&gt;Git will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;php artisan test&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;tests pass&lt;/strong&gt; → commit goes through&lt;/li&gt;
&lt;li&gt;If &lt;strong&gt;tests fail&lt;/strong&gt; → commit is &lt;strong&gt;blocked&lt;/strong&gt;&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  5. &lt;strong&gt;Git Submodules – Managing Dependencies&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Sometimes, your project &lt;strong&gt;depends on another repository&lt;/strong&gt;, like a shared library or plugin, that lives in a &lt;strong&gt;separate Git repo&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Git submodules&lt;/strong&gt; let you &lt;strong&gt;add that repo into your project&lt;/strong&gt; while keeping it separate and version-controlled.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-Life Example: Building with LEGO Kits
&lt;/h3&gt;

&lt;p&gt;You're building a LEGO &lt;strong&gt;castle&lt;/strong&gt;. But instead of building the &lt;strong&gt;horse stable&lt;/strong&gt; from scratch, you decide to buy a &lt;strong&gt;separate LEGO horse kit&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Main castle = your main project (main repo)&lt;/li&gt;
&lt;li&gt;Horse stable kit = shared module (external repo)&lt;/li&gt;
&lt;li&gt;You place the horse stable inside your castle, &lt;strong&gt;but it’s still a separate box&lt;/strong&gt; you can update or replace anytime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s what a &lt;strong&gt;submodule&lt;/strong&gt; is!&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Use It
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Add the submodule
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;submodule&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="nt"&gt;https&lt;/span&gt;&lt;span class="o"&gt;://&lt;/span&gt;&lt;span class="nt"&gt;github&lt;/span&gt;&lt;span class="nc"&gt;.com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;example&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;lib-utils&lt;/span&gt;&lt;span class="nc"&gt;.git&lt;/span&gt; &lt;span class="nt"&gt;libs&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;utils&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Initialize and pull submodule
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;submodule&lt;/span&gt; &lt;span class="nt"&gt;update&lt;/span&gt; &lt;span class="nt"&gt;--init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;nbsp;This downloads the actual code into your project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Points:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Submodules &lt;strong&gt;point to a specific commit&lt;/strong&gt; of the external repo, not the latest one.&lt;/li&gt;
&lt;li&gt;If the original library updates, your project &lt;strong&gt;won’t auto-update&lt;/strong&gt;, you choose &lt;strong&gt;when&lt;/strong&gt; to update it.&lt;/li&gt;
&lt;li&gt;It’s &lt;strong&gt;still a separate Git repo&lt;/strong&gt;, inside your repo.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6.&amp;nbsp;&lt;strong&gt;Understanding HEAD, Index, and Working Directory&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Think of Git as a 3-layer system&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Working Directory&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Index (Staging Area)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;HEAD&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Working Directory&lt;/strong&gt; (Your Current Work)
&lt;/h3&gt;

&lt;p&gt;This is where you &lt;strong&gt;edit your files&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You’re writing a document in Word or coding in VS Code, this is your &lt;strong&gt;working area&lt;/strong&gt;. The changes are only on your computer for now.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Index&lt;/strong&gt; (Staging Area)
&lt;/h3&gt;

&lt;p&gt;This is where you &lt;strong&gt;prepare changes&lt;/strong&gt; before saving them in history.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You finish writing one section of your report and put it in a "Ready to Print" folder. It’s not printed yet, but it’s&amp;nbsp;&lt;strong&gt;ready&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="nt"&gt;file&lt;/span&gt;&lt;span class="nc"&gt;.txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that file is &lt;strong&gt;staged&lt;/strong&gt; (put into the Index).&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;HEAD&lt;/strong&gt; (Last Commit – Git History)
&lt;/h3&gt;

&lt;p&gt;This is your &lt;strong&gt;latest saved version&lt;/strong&gt; in Git, the last commit you made.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Your report was printed yesterday, that’s the &lt;strong&gt;HEAD&lt;/strong&gt; version. If someone asks, “What did you last submit?”, you’ll give them the HEAD copy.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  View the Differences:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;status&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What’s &lt;strong&gt;changed but not staged&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;What’s &lt;strong&gt;staged but not committed&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;What’s already in the &lt;strong&gt;HEAD (last commit)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So every time you work with Git, you’re &lt;strong&gt;moving files between these 3 layers&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Modify (Working Directory)&lt;/li&gt;
&lt;li&gt;Stage with &lt;code&gt;git add&lt;/code&gt; (Index)&lt;/li&gt;
&lt;li&gt;Save with &lt;code&gt;git commit&lt;/code&gt; (HEAD)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Git is more than just version control,&amp;nbsp;it's a toolbox for collaboration, automation, and code safety. Mastering these advanced tools can drastically improve your efficiency, team collaboration, and confidence during critical development tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Step:&lt;/strong&gt;&lt;br&gt;
Missed the basics? &lt;a href="https://laraveldailytips.com/git-for-beginners-a-complete-getting-started-guide-with-examples" rel="noopener noreferrer"&gt;Git for Beginners&lt;/a&gt;&lt;br&gt;
Or continue from intermediate level: &lt;a href="https://laraveldailytips.com/git-for-intermediate" rel="noopener noreferrer"&gt;Git Intermediate Guide&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Git for Beginners – A Complete Getting Started Guide with Examples</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Wed, 30 Jul 2025 15:32:29 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/git-for-beginners-a-complete-getting-started-guide-with-examples-5fda</link>
      <guid>https://dev.to/laravel_dailytips/git-for-beginners-a-complete-getting-started-guide-with-examples-5fda</guid>
      <description>&lt;p&gt;Whether you're a student, a new developer, or just curious about coding, &lt;strong&gt;Git&lt;/strong&gt; is something you’ll hear about often. This guide is written for &lt;strong&gt;absolute beginners&lt;/strong&gt;. We’ll walk you through the &lt;strong&gt;basics of Git&lt;/strong&gt;, how to install it, set it up, and use it with simple commands and real examples.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. What is Git and Why Use It?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Git&lt;/strong&gt; is a version control system (VCS). It helps developers keep track of changes in their code. If something goes wrong, Git allows you to go back to a previous version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why use Git?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Track every change in your project&lt;/li&gt;
&lt;li&gt;Work with teams easily&lt;/li&gt;
&lt;li&gt;Prevent accidental data loss&lt;/li&gt;
&lt;li&gt;Collaborate using platforms like GitHub&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Version Control Basics
&lt;/h3&gt;

&lt;p&gt;There are two main types of version control:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Local VCS&lt;/strong&gt;: Tracks changes on your computer only&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Distributed VCS (like Git)&lt;/strong&gt;: Every developer has a full copy of the project history&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With Git, every change is recorded. You can see what was changed, when, and by whom.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Installing Git on Windows, macOS, and Linux
&lt;/h3&gt;

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

&lt;ol&gt;
&lt;li&gt;Download Git from &lt;a href="https://git-scm.com/download/win" rel="noopener noreferrer"&gt;https://git-scm.com/download/win&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Run the installer and follow the default steps&lt;/li&gt;
&lt;/ol&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;brew&lt;/span&gt; &lt;span class="nt"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;git&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(You need to have Homebrew installed)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Linux (Ubuntu/Debian)&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;sudo&lt;/span&gt; &lt;span class="nt"&gt;apt&lt;/span&gt; &lt;span class="nt"&gt;update&lt;/span&gt;
&lt;span class="nt"&gt;sudo&lt;/span&gt; &lt;span class="nt"&gt;apt&lt;/span&gt; &lt;span class="nt"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;git&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installation, check if Git is working:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Setting Up Git (username, email)
&lt;/h3&gt;

&lt;p&gt;Before using Git, set your name and email:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;config&lt;/span&gt; &lt;span class="nt"&gt;--global&lt;/span&gt; &lt;span class="nt"&gt;user&lt;/span&gt;&lt;span class="nc"&gt;.name&lt;/span&gt; &lt;span class="s1"&gt;"Your Name"&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;config&lt;/span&gt; &lt;span class="nt"&gt;--global&lt;/span&gt; &lt;span class="nt"&gt;user&lt;/span&gt;&lt;span class="nc"&gt;.email&lt;/span&gt; &lt;span class="s1"&gt;"you@example.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can verify:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;config&lt;/span&gt; &lt;span class="nt"&gt;--list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Creating a Repository (git init)
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;repository&lt;/strong&gt; is like a folder that Git tracks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;my-project&lt;/span&gt;
&lt;span class="nt"&gt;cd&lt;/span&gt; &lt;span class="nt"&gt;my-project&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;init&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You now have a Git repository in the &lt;code&gt;my-project&lt;/code&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Cloning a Repository (git clone)
&lt;/h3&gt;

&lt;p&gt;Cloning means downloading a remote Git project to your computer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;clone&lt;/span&gt; &lt;span class="nt"&gt;https&lt;/span&gt;&lt;span class="o"&gt;://&lt;/span&gt;&lt;span class="nt"&gt;github&lt;/span&gt;&lt;span class="nc"&gt;.com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;username&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;repo-name&lt;/span&gt;&lt;span class="nc"&gt;.git&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. Git File Lifecycle: Untracked → Staged → Committed
&lt;/h3&gt;

&lt;p&gt;Git tracks file changes in 3 steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Untracked&lt;/strong&gt;: New files Git doesn’t know about yet&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Staged&lt;/strong&gt;: Files marked to be saved&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Committed&lt;/strong&gt;: Files are saved in Git history&lt;/li&gt;
&lt;/ul&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;touch&lt;/span&gt; &lt;span class="nt"&gt;index&lt;/span&gt;&lt;span class="nc"&gt;.html&lt;/span&gt;                     &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;untracked&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="nt"&gt;index&lt;/span&gt;&lt;span class="nc"&gt;.html&lt;/span&gt;                   &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;staged&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"Add index file"&lt;/span&gt;       &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;committed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Staging Changes (git add)
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;git add&lt;/code&gt; to tell Git which files you want to track.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="nt"&gt;file1&lt;/span&gt;&lt;span class="nc"&gt;.html&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;To&lt;/span&gt; &lt;span class="nt"&gt;Add&lt;/span&gt; &lt;span class="nt"&gt;all&lt;/span&gt; &lt;span class="nt"&gt;files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9. Committing Changes (git commit)
&lt;/h3&gt;

&lt;p&gt;Once staged, commit your changes with a message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"Added homepage layout"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10. Checking Status and History (git status, git log)
&lt;/h3&gt;

&lt;p&gt;To check current changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;status&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;To&lt;/span&gt; &lt;span class="nt"&gt;see&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;history&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;

&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll see details like author, date, and message.&lt;/p&gt;

&lt;h3&gt;
  
  
  11. Basic File Operations (git rm, git mv)
&lt;/h3&gt;

&lt;p&gt;To delete a file from Git and your folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;unwanted&lt;/span&gt;&lt;span class="nc"&gt;.txt&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"Remove unwanted file"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To rename or move a file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;mv&lt;/span&gt; &lt;span class="nt"&gt;oldname&lt;/span&gt;&lt;span class="nc"&gt;.txt&lt;/span&gt; &lt;span class="nt"&gt;newname&lt;/span&gt;&lt;span class="nc"&gt;.txt&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"Renamed file"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  12. Creating Your First .gitignore File
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;.gitignore&lt;/code&gt; file tells Git which files or folders to skip.&lt;/p&gt;

&lt;p&gt;Example &lt;code&gt;.gitignore&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;node_modules&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="nc"&gt;.env&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;.log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create the file in your root directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;touch&lt;/span&gt; &lt;span class="nc"&gt;.gitignore&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  13. 🌐 Using Git with GitHub (Basics)
&lt;/h3&gt;

&lt;p&gt;GitHub is a website that stores Git repositories online. Here’s how to push your local code to GitHub:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Create a new repository on &lt;a href="https://github.com" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Connect local project with GitHub:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;remote&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt; &lt;span class="nt"&gt;https&lt;/span&gt;&lt;span class="o"&gt;://&lt;/span&gt;&lt;span class="nt"&gt;github&lt;/span&gt;&lt;span class="nc"&gt;.com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;yourusername&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;my-project&lt;/span&gt;&lt;span class="nc"&gt;.git&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;branch&lt;/span&gt; &lt;span class="nt"&gt;-M&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;push&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Now you know the basics of Git, how it works, and how to use it daily. You’ve learned how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up Git on any system&lt;/li&gt;
&lt;li&gt;Create and clone repositories&lt;/li&gt;
&lt;li&gt;Track changes using &lt;code&gt;git add&lt;/code&gt; and &lt;code&gt;git commit&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Work with GitHub&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is just the beginning. In the next blog, we’ll go deeper into &lt;strong&gt;branches, merging, and resolving conflicts&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Want more Laravel tips?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Visit &lt;a href="https://laraveldailytips.com/" rel="noopener noreferrer"&gt;LaravelDailyTips&lt;/a&gt; for practical guides, interview questions, and tricks.&lt;/p&gt;

&lt;p&gt;Subscribe now and get battle-tested Laravel insights delivered to your inbox before anyone else!&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>Git for Intermediate – Branching, Remotes, Undoing Changes &amp; More</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Wed, 30 Jul 2025 15:14:46 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/git-for-intermediate-branching-remotes-undoing-changes-more-pda</link>
      <guid>https://dev.to/laravel_dailytips/git-for-intermediate-branching-remotes-undoing-changes-more-pda</guid>
      <description>&lt;p&gt;Once you're familiar with basic Git commands, it's time to explore more practical and real-world usage. Git isn't just about adding and committing files, it's a powerful tool that helps teams collaborate, manage versions, and control the development flow with ease. In this guide, we'll walk you through&amp;nbsp;&lt;strong&gt;intermediate Git concepts&lt;/strong&gt; like &lt;strong&gt;branching&lt;/strong&gt;, &lt;strong&gt;working with remotes&lt;/strong&gt;, &lt;strong&gt;handling merge conflicts&lt;/strong&gt;, &lt;strong&gt;undoing mistakes&lt;/strong&gt;, &lt;strong&gt;tagging versions&lt;/strong&gt;, and even &lt;strong&gt;contributing to open-source projects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each topic is explained in &lt;strong&gt;simple with real examples&lt;/strong&gt;, so you can confidently apply these skills in day-to-day development. Whether you're working solo or in a team, mastering these intermediate Git commands will level up your workflow, reduce errors, and make your version control cleaner and more professional.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Branching in Git – What and Why
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What is branching?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Branching lets you create a separate line of development so you can work on new features or fixes without disturbing the main project. It helps teams work on multiple features at once. Each branch is like a safe space to try out ideas. Once done, you can merge it back into the main code.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Think of it like creating a copy of your code to experiment with.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  2. Creating and Switching Branches
&lt;/h3&gt;

&lt;p&gt;You can create a branch using &lt;code&gt;git branch branch-name&lt;/code&gt; and switch to it using &lt;code&gt;git switch&lt;/code&gt; or &lt;code&gt;git checkout&lt;/code&gt;. This is helpful when you want to work on something new without changing your main files. Git lets you jump between branches easily. This way, each task can live in its own workspace.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Create&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;new&lt;/span&gt; &lt;span class="nt"&gt;branch&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;branch&lt;/span&gt; &lt;span class="nt"&gt;feature-login&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Switch&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;that&lt;/span&gt; &lt;span class="nt"&gt;branch&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt; &lt;span class="nt"&gt;feature-login&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Or&lt;/span&gt; &lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;modern&lt;/span&gt; &lt;span class="nt"&gt;switch&lt;/span&gt; &lt;span class="nt"&gt;command&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;switch&lt;/span&gt; &lt;span class="nt"&gt;feature-login&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Merging Branches
&lt;/h3&gt;

&lt;p&gt;After finishing work in a feature branch, you can merge it into the main branch. Use &lt;code&gt;git merge branch-name&lt;/code&gt; to combine changes. If there are no conflicts, Git does this automatically. Merging keeps your main code updated with new features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;First&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;switch&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;switch&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Then&lt;/span&gt; &lt;span class="nt"&gt;merge&lt;/span&gt; &lt;span class="nt"&gt;your&lt;/span&gt; &lt;span class="nt"&gt;branch&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;merge&lt;/span&gt; &lt;span class="nt"&gt;feature-login&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Merge Conflicts – How to Resolve Them
&lt;/h3&gt;

&lt;p&gt;Merge conflicts happen when Git can’t decide which changes to keep from two branches. You’ll see conflict markers in the file that you need to edit manually. After fixing the content, save the file and commit the changes. This tells Git that the conflict is resolved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;You&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="nt"&gt;ll&lt;/span&gt; &lt;span class="nt"&gt;see&lt;/span&gt; &lt;span class="nt"&gt;conflict&lt;/span&gt; &lt;span class="nt"&gt;markers&lt;/span&gt; &lt;span class="nt"&gt;like&lt;/span&gt; &lt;span class="nt"&gt;this&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;file&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;
&lt;span class="nt"&gt;code&lt;/span&gt; &lt;span class="nt"&gt;from&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;
&lt;span class="o"&gt;=======&lt;/span&gt;
&lt;span class="nt"&gt;code&lt;/span&gt; &lt;span class="nt"&gt;from&lt;/span&gt; &lt;span class="nt"&gt;feature&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;feature-login&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to fix:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manually edit and keep the correct code.&lt;/li&gt;
&lt;li&gt;Remove the conflict markers.&lt;/li&gt;
&lt;li&gt;Save and commit.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"Resolved merge conflict"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Remote Repositories – Add, Fetch, Push, Pull
&lt;/h3&gt;

&lt;p&gt;Remote repositories are stored online (like on GitHub or GitLab). You can add a remote using &lt;code&gt;git remote add origin &amp;lt;url&amp;gt;&lt;/code&gt;, then push or pull code as needed. &lt;code&gt;git push&lt;/code&gt; uploads your changes, and &lt;code&gt;git pull&lt;/code&gt; brings in updates from others. This is how you collaborate with a team.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Add&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;remote&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;remote&lt;/span&gt; &lt;span class="nt"&gt;add&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt; &lt;span class="nt"&gt;https&lt;/span&gt;&lt;span class="o"&gt;://&lt;/span&gt;&lt;span class="nt"&gt;github&lt;/span&gt;&lt;span class="nc"&gt;.com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;yourusername&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;repo&lt;/span&gt;&lt;span class="nc"&gt;.git&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Push&lt;/span&gt; &lt;span class="nt"&gt;your&lt;/span&gt; &lt;span class="nt"&gt;branch&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;push&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Pull&lt;/span&gt; &lt;span class="nt"&gt;changes&lt;/span&gt; &lt;span class="nt"&gt;from&lt;/span&gt; &lt;span class="nt"&gt;remote&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;pull&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt; &lt;span class="nt"&gt;main&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Fetch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;download&lt;/span&gt; &lt;span class="nt"&gt;but&lt;/span&gt; &lt;span class="nt"&gt;don&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="nt"&gt;t&lt;/span&gt; &lt;span class="nt"&gt;merge&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;fetch&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Tracking vs Non-Tracking Branches
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tracking branch&lt;/strong&gt;: A tracking branch knows which remote branch it’s linked to and fetches updates automatically&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Non-tracking branch&lt;/strong&gt;: Non-tracking branches don’t have this link, so you need to push/pull manually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can set a tracking branch using the below command. This makes remote work easier.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Set&lt;/span&gt; &lt;span class="nt"&gt;upstream&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;tracking&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;push&lt;/span&gt; &lt;span class="nt"&gt;-u&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt; &lt;span class="nt"&gt;feature-login&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. &lt;strong&gt;Undoing Changes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Git allows you to undo changes at different stages. If you changed a file but didn’t stage it, use &lt;code&gt;git checkout -- filename&lt;/code&gt; to revert. To unstage a file, use &lt;code&gt;git reset filename&lt;/code&gt;. You can also undo commits with &lt;code&gt;git reset&lt;/code&gt; or update the last commit using &lt;code&gt;git commit --amend&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Undo Unstaged Changes
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;checkout&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;filename&lt;/span&gt;&lt;span class="nc"&gt;.txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Undo Staged Changes (but keep file)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reset&lt;/span&gt; &lt;span class="nt"&gt;filename&lt;/span&gt;&lt;span class="nc"&gt;.txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Undo Last Commit
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Keep&lt;/span&gt; &lt;span class="nt"&gt;changes&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;working&lt;/span&gt; &lt;span class="nt"&gt;directory&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reset&lt;/span&gt; &lt;span class="nt"&gt;--soft&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="err"&gt;1&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Remove&lt;/span&gt; &lt;span class="nt"&gt;changes&lt;/span&gt; &lt;span class="nt"&gt;too&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;reset&lt;/span&gt; &lt;span class="nt"&gt;--hard&lt;/span&gt; &lt;span class="nt"&gt;HEAD&lt;/span&gt;&lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="err"&gt;1&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Or&lt;/span&gt; &lt;span class="nt"&gt;amend&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;last&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;commit&lt;/span&gt; &lt;span class="nt"&gt;--amend&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"New commit message"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. &lt;strong&gt;Tagging Commits&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Tags are like labels used to mark important commits, such as a version release. Use &lt;code&gt;git tag v1.0&lt;/code&gt; to tag a commit. This makes it easy to go back to a specific version later. Tags are useful in deployments and version tracking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&amp;nbsp;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Create&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;tag&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;tag&lt;/span&gt; &lt;span class="nt"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Push&lt;/span&gt; &lt;span class="nt"&gt;tag&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;remote&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;push&lt;/span&gt; &lt;span class="nt"&gt;origin&lt;/span&gt; &lt;span class="nt"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  9. &lt;strong&gt;Stashing Work&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Stashing temporarily saves your uncommitted work. This is helpful if you need to switch branches quickly but aren’t ready to commit. Use &lt;code&gt;git stash&lt;/code&gt; to save, and &lt;code&gt;git stash apply&lt;/code&gt; to bring it back later. It’s like putting your work in a drawer for a moment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Save&lt;/span&gt; &lt;span class="nt"&gt;your&lt;/span&gt; &lt;span class="nt"&gt;work&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;with&lt;/span&gt; &lt;span class="nt"&gt;message&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;push&lt;/span&gt; &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s1"&gt;"this is new file"&lt;/span&gt;
&lt;span class="nf"&gt;#OR&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;save&lt;/span&gt; &lt;span class="s1"&gt;"this is new file"&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;See&lt;/span&gt; &lt;span class="nt"&gt;stashes&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;list&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Apply&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;last&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;apply&lt;/span&gt;
&lt;span class="nf"&gt;#OR&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;apply&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;n&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;OR&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;pop&lt;/span&gt;

&lt;span class="nf"&gt;#OR&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt; &lt;span class="nt"&gt;pop&lt;/span&gt; &lt;span class="nt"&gt;stash&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;n&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  10.&amp;nbsp;&lt;strong&gt;Comparing Changes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To see what you’ve changed, use &lt;code&gt;git diff&lt;/code&gt;. It shows the difference between files or commits. &lt;code&gt;git log --oneline&lt;/code&gt; gives a quick summary of past commits. These tools help you understand your project's history and code changes.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;diff&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;comparison&lt;/span&gt; &lt;span class="nt"&gt;of&lt;/span&gt; &lt;span class="nt"&gt;two&lt;/span&gt; &lt;span class="nt"&gt;commits&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;diff&lt;/span&gt; &lt;span class="nt"&gt;commit_one&lt;/span&gt; &lt;span class="nt"&gt;commit_two&lt;/span&gt;

&lt;span class="nf"&gt;#OR&lt;/span&gt;
&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;log&lt;/span&gt; &lt;span class="nt"&gt;--oneline&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  11. &lt;strong&gt;Working with .gitattributes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;.gitattributes&lt;/code&gt; file controls how Git handles different file types. You can use it to manage line endings or define merge rules. It helps avoid issues when working across different operating systems. It’s especially useful in team environments.&lt;/p&gt;

&lt;p&gt;Example &lt;code&gt;.gitattributes&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;.txt&lt;/span&gt; &lt;span class="nt"&gt;text&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;.jpg&lt;/span&gt; &lt;span class="nt"&gt;binary&lt;/span&gt;
&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt; &lt;span class="nt"&gt;diff&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  12. &lt;strong&gt;Contributing to Remote Projects&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;To contribute to open-source projects, you usually fork the repo, then clone it to your computer. Make changes in a new branch, push them, and create a pull request. This process is called "fork → clone → pull request." It’s the standard way to suggest improvements to someone else’s project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;git&lt;/span&gt; &lt;span class="nt"&gt;clone&lt;/span&gt; &lt;span class="nt"&gt;https&lt;/span&gt;&lt;span class="o"&gt;://&lt;/span&gt;&lt;span class="nt"&gt;github&lt;/span&gt;&lt;span class="nc"&gt;.com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;yourusername&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;project&lt;/span&gt;&lt;span class="nc"&gt;.git&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read more:&lt;/strong&gt; If you're just getting started, check out my &lt;a href="https://laraveldailytips.com/git-for-beginners-a-complete-getting-started-guide-with-examples" rel="noopener noreferrer"&gt;Git for Beginners – A Complete Getting Started Guide&lt;/a&gt; to learn the basics before diving into these intermediate-level concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;By now, you’ve covered all the major intermediate Git concepts that developers use every day. Keep practicing with your own or open-source projects. These skills will help you collaborate efficiently and maintain clean project histories.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>How to Log into a Custom Log File in Laravel</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Tue, 29 Jul 2025 19:26:25 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/how-to-log-into-a-custom-log-file-in-laravel-kjk</link>
      <guid>https://dev.to/laravel_dailytips/how-to-log-into-a-custom-log-file-in-laravel-kjk</guid>
      <description>&lt;p&gt;Laravel, by default, writes logs into a single &lt;code&gt;laravel.log&lt;/code&gt; file in &lt;code&gt;storage/logs&lt;/code&gt;. But what if you want to separate your logs, maybe to track a specific module like payments or API calls?&lt;/p&gt;

&lt;p&gt;In this guide, you’ll learn how to &lt;strong&gt;log to a separate file&lt;/strong&gt;, followed by &lt;strong&gt;advanced tips&lt;/strong&gt; for better log management in Laravel.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Define a New Log Channel
&lt;/h3&gt;

&lt;p&gt;Open &lt;code&gt;config/logging.php&lt;/code&gt; and define your custom log channel inside the &lt;code&gt;channels&lt;/code&gt; array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="s2"&gt;'channels'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;

    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="o"&gt;...&lt;/span&gt; &lt;span class="nt"&gt;other&lt;/span&gt; &lt;span class="nt"&gt;channels&lt;/span&gt;

    &lt;span class="s2"&gt;'custom_payment'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
        &lt;span class="s2"&gt;'driver'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'single'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;'path'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;storage_path&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'logs/payment.log'&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
        &lt;span class="s2"&gt;'level'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'debug'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;],&lt;/span&gt;
&lt;span class="o"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells Laravel to create a new log file at:&lt;br&gt;
&lt;code&gt;storage/logs/payment.log&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Log into the Custom Channel
&lt;/h3&gt;

&lt;p&gt;Now, in your code, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Log&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;Log&lt;/span&gt;&lt;span class="nd"&gt;::channel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'custom_payment'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'Payment process started for order ID: 12345'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s it! This will write logs only to &lt;code&gt;payment.log&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Tips (2025-Level)
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. &lt;strong&gt;Use Daily Logs for Rotation&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Prevent massive log files with daily log rotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="s2"&gt;'custom_payment'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;'driver'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'daily'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'path'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;storage_path&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'logs/payment.log'&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
    &lt;span class="s2"&gt;'level'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'debug'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'days'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;14&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;keep&lt;/span&gt; &lt;span class="nt"&gt;logs&lt;/span&gt; &lt;span class="nt"&gt;for&lt;/span&gt; &lt;span class="err"&gt;14&lt;/span&gt; &lt;span class="nt"&gt;days&lt;/span&gt;
&lt;span class="o"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. &lt;strong&gt;Format Log Output (Customize Log Format)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;To make logs more readable or add contextual information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="s2"&gt;'custom_payment'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;'driver'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'monolog'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'handler'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;Monolog&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Handler&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;StreamHandler&lt;/span&gt;&lt;span class="nd"&gt;::class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'formatter'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;Monolog&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Formatter&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;LineFormatter&lt;/span&gt;&lt;span class="nd"&gt;::class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'with'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
        &lt;span class="s2"&gt;'stream'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;storage_path&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'logs/payment.log'&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
    &lt;span class="o"&gt;],&lt;/span&gt;
    &lt;span class="s2"&gt;'formatter_with'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
        &lt;span class="s2"&gt;'format'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;"[%datetime%] %channel%.%level_name%: %message% %context%\n"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;'dateFormat'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'Y-m-d H:i:s'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;],&lt;/span&gt;
&lt;span class="o"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then log something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;Log&lt;/span&gt;&lt;span class="nd"&gt;::channel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'custom_payment'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'Payment processed'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;'order_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;98765&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'user_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;123&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Output in &lt;code&gt;payment.log&lt;/code&gt; will look like:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="err"&gt;2025&lt;/span&gt;&lt;span class="nt"&gt;-07-29&lt;/span&gt; &lt;span class="err"&gt;19&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;45&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;32&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="nt"&gt;custom_payment&lt;/span&gt;&lt;span class="nc"&gt;.INFO&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;Payment&lt;/span&gt; &lt;span class="nt"&gt;processed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;"order_id":98765,"user_id":123&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. &lt;strong&gt;Log Based on Environment&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Use &lt;code&gt;.env&lt;/code&gt; to dynamically set the log channel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;LOG_CHANNEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;custom_payment&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;code&gt;logging.php&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="s2"&gt;'default'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;env&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'LOG_CHANNEL'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'stack'&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This helps switch between channels in local, staging, or production environments easily.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. &lt;strong&gt;Contextual Logging&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Use context data to attach extra info:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;Log&lt;/span&gt;&lt;span class="nd"&gt;::channel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'custom_payment'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'Payment completed'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;'user_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;auth&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt;
    &lt;span class="s2"&gt;'order_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;123&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;'amount'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;500&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is especially useful when debugging API calls or tracing actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Log Viewer Package
&lt;/h2&gt;

&lt;p&gt;To visually inspect logs in your browser:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;composer&lt;/span&gt; &lt;span class="nt"&gt;require&lt;/span&gt; &lt;span class="nt"&gt;opcodesio&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;log-viewer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visit &lt;code&gt;/log-viewer&lt;/code&gt; in your browser. It supports filtering, searching, and more!&lt;/p&gt;

&lt;p&gt;🔗 &lt;a href="https://github.com/opcodesio/log-viewer" rel="noopener noreferrer"&gt;Log Viewer by opcodesio&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Laravel’s logging system is powerful and highly customizable. Whether you’re managing logs per module or setting up advanced Monolog formatting, these techniques will give you clean, organized, and maintainable logs.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Want more Laravel tips?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Visit &lt;a href="https://laraveldailytips.com/" rel="noopener noreferrer"&gt;LaravelDailyTips&lt;/a&gt; for practical guides, interview questions, and tricks.&lt;/p&gt;

&lt;p&gt;Original Blog &lt;a href="https://laraveldailytips.com/how-to-log-into-a-custom-log-file-in-laravel" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Subscribe now and get battle-tested Laravel insights delivered to your inbox before anyone else!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Laravel: Defensive programming strategies in 2025</title>
      <dc:creator>Laravel Daily tips</dc:creator>
      <pubDate>Tue, 29 Jul 2025 10:24:30 +0000</pubDate>
      <link>https://dev.to/laravel_dailytips/laravel-defensive-programming-strategies-in-2025-am4</link>
      <guid>https://dev.to/laravel_dailytips/laravel-defensive-programming-strategies-in-2025-am4</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;1. Always Keep Laravel &amp;amp; Dependencies Updated&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The first and most fundamental rule of Laravel security is to always keep your framework and its dependencies updated. Laravel's core team regularly releases security patches and bug fixes. Running outdated versions leaves your application vulnerable to known exploits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable Tip:&lt;/strong&gt; Regularly run &lt;code&gt;composer update&lt;/code&gt; and use tools like Dependabot or Renovate to automate dependency monitoring and updates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Check&lt;/span&gt; &lt;span class="nt"&gt;current&lt;/span&gt; &lt;span class="nt"&gt;Laravel&lt;/span&gt; &lt;span class="nt"&gt;version&lt;/span&gt;
&lt;span class="nt"&gt;php&lt;/span&gt; &lt;span class="nt"&gt;artisan&lt;/span&gt; &lt;span class="nt"&gt;--version&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Update&lt;/span&gt; &lt;span class="nt"&gt;Composer&lt;/span&gt; &lt;span class="nt"&gt;dependencies&lt;/span&gt;
&lt;span class="nt"&gt;composer&lt;/span&gt; &lt;span class="nt"&gt;update&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Update&lt;/span&gt; &lt;span class="nt"&gt;Laravel&lt;/span&gt; &lt;span class="nt"&gt;specifically&lt;/span&gt;
&lt;span class="nt"&gt;composer&lt;/span&gt; &lt;span class="nt"&gt;update&lt;/span&gt; &lt;span class="nt"&gt;laravel&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;framework&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;For&lt;/span&gt; &lt;span class="nt"&gt;major&lt;/span&gt; &lt;span class="nt"&gt;version&lt;/span&gt; &lt;span class="nt"&gt;updates&lt;/span&gt;
&lt;span class="nt"&gt;composer&lt;/span&gt; &lt;span class="nt"&gt;require&lt;/span&gt; &lt;span class="nt"&gt;laravel&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;framework&lt;/span&gt;&lt;span class="o"&gt;:^&lt;/span&gt;&lt;span class="err"&gt;11&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Automated Security Updates
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;composer&lt;/span&gt;&lt;span class="nc"&gt;.json&lt;/span&gt; &lt;span class="nt"&gt;-&lt;/span&gt; &lt;span class="nt"&gt;Configure&lt;/span&gt; &lt;span class="nt"&gt;automatic&lt;/span&gt; &lt;span class="nt"&gt;security&lt;/span&gt; &lt;span class="nt"&gt;updates&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;"config":&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;"audit":&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;"abandoned":&lt;/span&gt; &lt;span class="err"&gt;"report"&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;"scripts"&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;"security-check":&lt;/span&gt; &lt;span class="err"&gt;"composer&lt;/span&gt; &lt;span class="err"&gt;audit"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;2. Use Environment Variables for Secrets&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Your &lt;code&gt;.env&lt;/code&gt; file contains sensitive configuration details. Debug mode, when enabled in production, can leak critical information about your application's internals, aiding attackers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable Tip:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Never commit your &lt;code&gt;.env&lt;/code&gt; file to version control.&lt;/li&gt;
&lt;li&gt;Ensure &lt;code&gt;APP_ENV=production&lt;/code&gt; and &lt;code&gt;APP_DEBUG=false&lt;/code&gt; in your production &lt;code&gt;.env&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;LOG_LEVEL=error&lt;/code&gt; or &lt;code&gt;LOG_LEVEL=critical&lt;/code&gt; in production to prevent sensitive data from appearing in logs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example (&lt;/strong&gt;&lt;code&gt;.env&lt;/code&gt;&lt;strong&gt; file in production):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;APP_ENV&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;production&lt;/span&gt;
&lt;span class="nt"&gt;APP_DEBUG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;false&lt;/span&gt;
&lt;span class="nt"&gt;APP_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;base64&lt;/span&gt;&lt;span class="nd"&gt;:YOUR_VERY_SECURE_APP_KEY_GENERATED_BY_ARTISAN&lt;/span&gt;

&lt;span class="nt"&gt;LOG_CHANNEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;stack&lt;/span&gt;
&lt;span class="nt"&gt;LOG_LEVEL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;error&lt;/span&gt;

&lt;span class="nt"&gt;DB_CONNECTION&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;mysql&lt;/span&gt;
&lt;span class="nt"&gt;DB_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;127&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="err"&gt;1&lt;/span&gt;
&lt;span class="nt"&gt;DB_PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;3306&lt;/span&gt;
&lt;span class="nt"&gt;DB_DATABASE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;your_production_db&lt;/span&gt;
&lt;span class="nt"&gt;DB_USERNAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;your_production_user&lt;/span&gt;
&lt;span class="nt"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;your_production_password&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;3. Use CSRF Protection&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Laravel 11 introduced significant changes to CSRF protection. Starting from Laravel 11, the VerifyCsrfToken middleware no longer exists within the application's skeleton, requiring a new approach to configuration.&lt;/p&gt;

&lt;h3&gt;
  
  
  New CSRF Configuration (Laravel 11 &amp;amp; 12)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;bootstrap&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;app&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;

&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Foundation&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Foundation&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Configuration&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Exceptions&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Foundation&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Configuration&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Middleware&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;return&lt;/span&gt; &lt;span class="nt"&gt;Application&lt;/span&gt;&lt;span class="nd"&gt;::configure&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;basePath&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;dirname&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;__DIR__&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
    &lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;withRouting&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
        &lt;span class="nt"&gt;web&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;__DIR__&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;'/../routes/web.php'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="nt"&gt;commands&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;__DIR__&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;'/../routes/console.php'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
        &lt;span class="nt"&gt;health&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;'/up'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;withMiddleware&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;Middleware&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;//&lt;/span&gt; &lt;span class="err"&gt;Configure&lt;/span&gt; &lt;span class="err"&gt;CSRF&lt;/span&gt; &lt;span class="err"&gt;token&lt;/span&gt; &lt;span class="err"&gt;validation&lt;/span&gt;
        &lt;span class="err"&gt;$middleware-&amp;gt;validateCsrfTokens(&lt;/span&gt;&lt;span class="py"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s2"&gt;'webhook/*'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s2"&gt;'api/external/*'&lt;/span&gt;
        &lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;withExceptions&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;Exceptions&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;exceptions&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;//&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;create&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CSRF Token in Blade Templates
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;!&lt;/span&gt;&lt;span class="nt"&gt;--&lt;/span&gt; &lt;span class="nt"&gt;Blade&lt;/span&gt; &lt;span class="nt"&gt;template&lt;/span&gt; &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt; &lt;span class="nt"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"POST"&lt;/span&gt; &lt;span class="nt"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"/profile"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;@csrf&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"text"&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"name"&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"{{ old('name') }}"&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="err"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"submit"&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;Update&lt;/span&gt; &lt;span class="n"&gt;Profile&lt;/span&gt;&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="err"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CSRF for AJAX Requests
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;Set&lt;/span&gt; &lt;span class="nt"&gt;up&lt;/span&gt; &lt;span class="nt"&gt;CSRF&lt;/span&gt; &lt;span class="nt"&gt;token&lt;/span&gt; &lt;span class="nt"&gt;for&lt;/span&gt; &lt;span class="nt"&gt;AJAX&lt;/span&gt; &lt;span class="nt"&gt;requests&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;.ajaxSetup&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;'X-CSRF-TOKEN'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'meta[name="csrf-token"]'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;attr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'content'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;Or&lt;/span&gt; &lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;XSRF-TOKEN&lt;/span&gt; &lt;span class="nt"&gt;cookie&lt;/span&gt;
&lt;span class="nt"&gt;axios&lt;/span&gt;&lt;span class="nc"&gt;.defaults.xsrfCookieName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;'XSRF-TOKEN'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;axios&lt;/span&gt;&lt;span class="nc"&gt;.defaults.xsrfHeaderName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;'X-XSRF-TOKEN'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;4. Sanitize Input &amp;amp; Prevent XSS&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Never trust user input. This is a golden rule of web security. Unvalidated input can lead to various attacks, including SQL injection and Cross-Site Scripting (XSS). Laravel's powerful validation rules and Blade's automatic escaping are your primary defenses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable Tip:&lt;/strong&gt; Validate all incoming request data. Use Blade's &lt;code&gt;{{ $variable }}&lt;/code&gt; for output escaping by default. Only use &lt;code&gt;{!! $variable !!}&lt;/code&gt; when you are absolutely certain the content is safe (e.g., purified HTML). For user-provided rich text, use a dedicated HTML purifier library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Example (Validation):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;

&lt;span class="nt"&gt;namespace&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Http&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Requests&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Foundation&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Http&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;FormRequest&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Validation&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Rules&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Password&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;UserRegistrationRequest&lt;/span&gt; &lt;span class="nt"&gt;extends&lt;/span&gt; &lt;span class="nt"&gt;FormRequest&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;rules():&lt;/span&gt; &lt;span class="err"&gt;array&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;return&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;
            &lt;span class="err"&gt;'name'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;['required',&lt;/span&gt; &lt;span class="err"&gt;'string',&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="py"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="m"&gt;255&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;:/&lt;/span&gt;&lt;span class="err"&gt;^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a-zA-Z&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;+$&lt;/span&gt;&lt;span class="p"&gt;/&lt;/span&gt;&lt;span class="s2"&gt;'],
            '&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; ['&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;rfc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;dns&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="n"&gt;unique&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="s2"&gt;'],
            '&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; [
                '&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="s2"&gt;',
                '&lt;/span&gt;&lt;span class="n"&gt;confirmed&lt;/span&gt;&lt;span class="s2"&gt;',
                Password::min(12)
                    -&amp;gt;letters()
                    -&amp;gt;mixedCase()
                    -&amp;gt;numbers()
                    -&amp;gt;symbols()
                    -&amp;gt;uncompromised()
            ],
            '&lt;/span&gt;&lt;span class="n"&gt;phone&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; ['&lt;/span&gt;&lt;span class="n"&gt;nullable&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="n"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;:/&lt;/span&gt;&lt;span class="err"&gt;^\+?&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1-9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="err"&gt;{&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;14&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="s2"&gt;'],
            '&lt;/span&gt;&lt;span class="nt"&gt;website&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; ['&lt;/span&gt;&lt;span class="nt"&gt;nullable&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="nt"&gt;url&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="nt"&gt;max&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;255&lt;/span&gt;&lt;span class="s2"&gt;'],
            '&lt;/span&gt;&lt;span class="nt"&gt;age&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; ['&lt;/span&gt;&lt;span class="nt"&gt;required&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="nt"&gt;integer&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="nt"&gt;min&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;18&lt;/span&gt;&lt;span class="s2"&gt;', '&lt;/span&gt;&lt;span class="nt"&gt;max&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="err"&gt;120&lt;/span&gt;&lt;span class="s2"&gt;'],
        ];
    }

    public function messages(): array
    {
        return [
            '&lt;/span&gt;&lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="nc"&gt;.regex&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; '&lt;/span&gt;&lt;span class="nt"&gt;Name&lt;/span&gt; &lt;span class="nt"&gt;can&lt;/span&gt; &lt;span class="nt"&gt;only&lt;/span&gt; &lt;span class="nt"&gt;contain&lt;/span&gt; &lt;span class="nt"&gt;letters&lt;/span&gt; &lt;span class="nt"&gt;and&lt;/span&gt; &lt;span class="nt"&gt;spaces&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;',
            '&lt;/span&gt;&lt;span class="nt"&gt;email&lt;/span&gt;&lt;span class="nc"&gt;.email&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; '&lt;/span&gt;&lt;span class="nt"&gt;Please&lt;/span&gt; &lt;span class="nt"&gt;provide&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;valid&lt;/span&gt; &lt;span class="nt"&gt;email&lt;/span&gt; &lt;span class="nt"&gt;address&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;',
            '&lt;/span&gt;&lt;span class="nt"&gt;password&lt;/span&gt;&lt;span class="nc"&gt;.uncompromised&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; '&lt;/span&gt;&lt;span class="nt"&gt;The&lt;/span&gt; &lt;span class="nt"&gt;password&lt;/span&gt; &lt;span class="nt"&gt;has&lt;/span&gt; &lt;span class="nt"&gt;been&lt;/span&gt; &lt;span class="nt"&gt;compromised&lt;/span&gt; &lt;span class="nt"&gt;in&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;data&lt;/span&gt; &lt;span class="nt"&gt;breach&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="s2"&gt;',
        ];
    }

    protected function prepareForValidation(): void
    {
        $this-&amp;gt;merge([
            '&lt;/span&gt;&lt;span class="nt"&gt;email&lt;/span&gt;&lt;span class="s2"&gt;' =&amp;gt; strtolower(trim($this-&amp;gt;email)),
            '&lt;/span&gt;&lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;trim&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;this-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;name&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
        &lt;span class="o"&gt;]);&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;5. Use Authorization Policies and Gates&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Laravel's built-in authentication system (powered by Laravel Fortify or Jetstream) is incredibly robust and handles many security concerns for you, including password hashing with Bcrypt, session management, and login throttling. For authorization, utilize Gates and Policies to define granular access control.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable Tip:&lt;/strong&gt; Do not roll your own authentication system unless absolutely necessary. Embrace Fortify or Jetstream for quick and secure scaffolding. Use Policies for resource-specific authorization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;php&lt;/span&gt; &lt;span class="nt"&gt;artisan&lt;/span&gt; &lt;span class="nt"&gt;make&lt;/span&gt;&lt;span class="nd"&gt;:policy&lt;/span&gt; &lt;span class="nt"&gt;PostPolicy&lt;/span&gt; &lt;span class="nt"&gt;--model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;Post&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;Policies&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;PostPolicy&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;

&lt;span class="nt"&gt;namespace&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Policies&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;User&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;PostPolicy&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;/**
     * Determine whether the user can update the post.
     */&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;update(User&lt;/span&gt; &lt;span class="err"&gt;$user,&lt;/span&gt; &lt;span class="err"&gt;Post&lt;/span&gt; &lt;span class="err"&gt;$post):&lt;/span&gt; &lt;span class="err"&gt;bool&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;return&lt;/span&gt; &lt;span class="err"&gt;$user-&amp;gt;id&lt;/span&gt; &lt;span class="err"&gt;===&lt;/span&gt; &lt;span class="err"&gt;$post-&amp;gt;user_id;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;/**
     * Determine whether the user can delete the post.
     */&lt;/span&gt;
    &lt;span class="nt"&gt;public&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;delete&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;User&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;user&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;Post&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;post&lt;/span&gt;&lt;span class="o"&gt;):&lt;/span&gt; &lt;span class="nt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;return&lt;/span&gt; &lt;span class="err"&gt;$user-&amp;gt;id&lt;/span&gt; &lt;span class="err"&gt;===&lt;/span&gt; &lt;span class="err"&gt;$post-&amp;gt;user_id;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;use it in a controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;Controllers&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;PostController&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;

&lt;span class="nt"&gt;namespace&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Http&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Controllers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Models&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Post&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Http&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Gate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;PostController&lt;/span&gt; &lt;span class="nt"&gt;extends&lt;/span&gt; &lt;span class="nt"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;update(Request&lt;/span&gt; &lt;span class="err"&gt;$request,&lt;/span&gt; &lt;span class="err"&gt;Post&lt;/span&gt; &lt;span class="err"&gt;$post)&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;if&lt;/span&gt; &lt;span class="err"&gt;(!&lt;/span&gt; &lt;span class="py"&gt;Gate&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;allows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'update'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;abort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;403&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'You are not authorized to update this post.'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;Update&lt;/span&gt; &lt;span class="nt"&gt;the&lt;/span&gt; &lt;span class="nt"&gt;post&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;6. Protect Routes with Middleware&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Always protect routes with &lt;code&gt;auth&lt;/code&gt; or custom middleware.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;Route&lt;/span&gt;&lt;span class="nd"&gt;::middleware&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;'auth:sanctum'&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;group&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'/dashboard'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;DashboardController&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'index'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use Laravel &lt;strong&gt;Sanctum&lt;/strong&gt; or &lt;strong&gt;Passport&lt;/strong&gt; for secure API authentication.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;7. Rate Limiting for APIs and Login&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Rate limiting is crucial for preventing brute-force attacks on login forms, API endpoints, or any resource that could be abused by repetitive requests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable Tip:&lt;/strong&gt; Utilize Laravel's built-in rate limiter middleware.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;web&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt; &lt;span class="nt"&gt;or&lt;/span&gt; &lt;span class="nt"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;api&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Cache&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;RateLimiting&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Limit&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;RateLimiter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Http&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;Define&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;custom&lt;/span&gt; &lt;span class="nt"&gt;rate&lt;/span&gt; &lt;span class="nt"&gt;limiter&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;optional&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;but&lt;/span&gt; &lt;span class="nt"&gt;good&lt;/span&gt; &lt;span class="nt"&gt;for&lt;/span&gt; &lt;span class="nt"&gt;fine-grained&lt;/span&gt; &lt;span class="nt"&gt;control&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;RateLimiter&lt;/span&gt;&lt;span class="nd"&gt;::for&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'login'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;Request&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;request&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;return&lt;/span&gt; &lt;span class="py"&gt;Limit&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;perMinute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;request-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'email'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;?&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;request-&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;Apply&lt;/span&gt; &lt;span class="nt"&gt;rate&lt;/span&gt; &lt;span class="nt"&gt;limiting&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;route&lt;/span&gt; &lt;span class="nt"&gt;group&lt;/span&gt;
&lt;span class="nt"&gt;Route&lt;/span&gt;&lt;span class="nd"&gt;::middleware&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;'throttle:login'&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;group&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="py"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'/login'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;AuthController&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'login'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;Or&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;single&lt;/span&gt; &lt;span class="nt"&gt;route&lt;/span&gt;
&lt;span class="nt"&gt;Route&lt;/span&gt;&lt;span class="nd"&gt;::get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'/api/data'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;ApiController&lt;/span&gt;&lt;span class="nd"&gt;::class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'getData'&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'throttle:60,1'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="err"&gt;60&lt;/span&gt; &lt;span class="nt"&gt;requests&lt;/span&gt; &lt;span class="nt"&gt;per&lt;/span&gt; &lt;span class="nt"&gt;minute&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;8. Use HTTPS &amp;amp; Force Secure Headers&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In 2025, there's simply no excuse not to use HTTPS. It encrypts data in transit, protecting against eavesdropping and Man-in-the-Middle (MitM) attacks. Strict-Transport-Security (HSTS) ensures browsers always connect via HTTPS, even if the user tries to access the site via HTTP.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable Tip:&lt;/strong&gt; Configure your web server (Nginx/Apache) to redirect all HTTP traffic to HTTPS, and force HTTPS within your Laravel application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Example (Force HTTPS in &lt;/strong&gt;&lt;code&gt;AppServiceProvider&lt;/code&gt;&lt;strong&gt;):&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;Providers&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;AppServiceProvider&lt;/span&gt;&lt;span class="nc"&gt;.php&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;

&lt;span class="nt"&gt;namespace&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Providers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;ServiceProvider&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;AppServiceProvider&lt;/span&gt; &lt;span class="nt"&gt;extends&lt;/span&gt; &lt;span class="nt"&gt;ServiceProvider&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c"&gt;/**
     * Register any application services.
     */&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;register():&lt;/span&gt; &lt;span class="err"&gt;void&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;//&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c"&gt;/**
     * Bootstrap any application services.
     */&lt;/span&gt;
    &lt;span class="nt"&gt;public&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;boot&lt;/span&gt;&lt;span class="o"&gt;():&lt;/span&gt; &lt;span class="nt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;if&lt;/span&gt; &lt;span class="err"&gt;($this-&amp;gt;app-&amp;gt;environment('production'))&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="py"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;forceScheme&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'https'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should also configure HSTS in your web server. For Nginx, this might look like:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;

&lt;span class="nt"&gt;namespace&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Http&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Middleware&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Closure&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;SecurityHeaders&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;handle($request,&lt;/span&gt; &lt;span class="err"&gt;Closure&lt;/span&gt; &lt;span class="err"&gt;$next)&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;$response&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$next($request);&lt;/span&gt;
        &lt;span class="err"&gt;$response-&amp;gt;headers-&amp;gt;set('X-Frame-Options',&lt;/span&gt; &lt;span class="err"&gt;'DENY');&lt;/span&gt;
        &lt;span class="err"&gt;$response-&amp;gt;headers-&amp;gt;set('X-Content-Type-Options',&lt;/span&gt; &lt;span class="err"&gt;'nosniff');&lt;/span&gt;
        &lt;span class="err"&gt;$response-&amp;gt;headers-&amp;gt;set('X-XSS-Protection',&lt;/span&gt; &lt;span class="err"&gt;'1;&lt;/span&gt; &lt;span class="err"&gt;mode=block');&lt;/span&gt;
        &lt;span class="err"&gt;$response-&amp;gt;headers-&amp;gt;set('Strict-Transport-Security',&lt;/span&gt; &lt;span class="err"&gt;'max-age=31536000;&lt;/span&gt; &lt;span class="err"&gt;includeSubDomains');&lt;/span&gt;
        &lt;span class="err"&gt;return&lt;/span&gt; &lt;span class="err"&gt;$response;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;9. Encrypt Sensitive Data&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Use Laravel’s &lt;code&gt;Crypt&lt;/code&gt; facade for storing sensitive information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Crypt&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nt"&gt;Crypt&lt;/span&gt;&lt;span class="nd"&gt;::encryptString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'MySecretData'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nt"&gt;Crypt&lt;/span&gt;&lt;span class="nd"&gt;::decryptString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;encrypted&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;10. Password Hashing and Reset Tokens&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Always hash passwords using Laravel's Hash facade.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;user-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nt"&gt;Hash&lt;/span&gt;&lt;span class="nd"&gt;::make&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;request-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;password&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don’t manually store or generate password reset tokens. Use Laravel's &lt;code&gt;Password&lt;/code&gt; broker.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;11. File Upload Protection&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Validate file uploads and store them securely.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;request-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;validate&lt;/span&gt;&lt;span class="o"&gt;([&lt;/span&gt;
    &lt;span class="s2"&gt;'image'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;'required|image|mimes:jpg,jpeg,png|max:2048'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="o"&gt;]);&lt;/span&gt;

&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;request-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;file&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'image'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;store&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;'uploads'&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;'public'&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Avoid exposing original file names and restrict access via signed URLs or controllers.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Regular Security Audits and Testing
&lt;/h2&gt;

&lt;p&gt;Security is not a one-time setup; it's an ongoing process. Regularly audit your application's code, configurations, and dependencies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Actionable Tip:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Conduct regular security penetration testing.&lt;/li&gt;
&lt;li&gt;Use static analysis tools (SAST) and dynamic analysis tools (DAST).&lt;/li&gt;
&lt;li&gt;Integrate &lt;code&gt;composer audit&lt;/code&gt; into your CI/CD pipeline.&lt;/li&gt;
&lt;li&gt;Consider using Laravel Pulse for real-time monitoring of application performance and security events (like failed logins).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;12. Set Proper Permissions on Server&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.env&lt;/code&gt; should never be world-readable.&lt;/li&gt;
&lt;li&gt;Set correct file permissions:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;chmod&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="err"&gt;755&lt;/span&gt; &lt;span class="nt"&gt;storage&lt;/span&gt; &lt;span class="nt"&gt;bootstrap&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;cache&lt;/span&gt;
&lt;span class="nt"&gt;chown&lt;/span&gt; &lt;span class="nt"&gt;-R&lt;/span&gt; &lt;span class="nt"&gt;www-data&lt;/span&gt;&lt;span class="nd"&gt;:www-data&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;fail2ban&lt;/strong&gt;, &lt;strong&gt;UFW&lt;/strong&gt;, and &lt;strong&gt;SSL&lt;/strong&gt; for server security.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  13. Environment Configuration Security
&lt;/h2&gt;

&lt;p&gt;Properly secure your environment configuration and sensitive data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nc"&gt;.env&lt;/span&gt;

&lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nt"&gt;Additional&lt;/span&gt; &lt;span class="nt"&gt;security&lt;/span&gt; &lt;span class="nt"&gt;headers&lt;/span&gt;
&lt;span class="nt"&gt;SECURE_HEADERS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuration Validation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="nt"&gt;php&lt;/span&gt;

&lt;span class="nt"&gt;namespace&lt;/span&gt; &lt;span class="nt"&gt;App&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Providers&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;ServiceProvider&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;use&lt;/span&gt; &lt;span class="nt"&gt;Illuminate&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Support&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Facades&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nt"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nt"&gt;class&lt;/span&gt; &lt;span class="nt"&gt;SecurityServiceProvider&lt;/span&gt; &lt;span class="nt"&gt;extends&lt;/span&gt; &lt;span class="nt"&gt;ServiceProvider&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;public&lt;/span&gt; &lt;span class="err"&gt;function&lt;/span&gt; &lt;span class="err"&gt;boot():&lt;/span&gt; &lt;span class="err"&gt;void&lt;/span&gt;
    &lt;span class="err"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;if&lt;/span&gt; &lt;span class="err"&gt;(app()-&amp;gt;environment('production'))&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;$this-&amp;gt;validateProductionConfig();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;

    &lt;span class="nt"&gt;private&lt;/span&gt; &lt;span class="nt"&gt;function&lt;/span&gt; &lt;span class="nt"&gt;validateProductionConfig&lt;/span&gt;&lt;span class="o"&gt;():&lt;/span&gt; &lt;span class="nt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="err"&gt;$requiredConfigs&lt;/span&gt; &lt;span class="err"&gt;=&lt;/span&gt; &lt;span class="err"&gt;[&lt;/span&gt;
            &lt;span class="err"&gt;'app.key'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;'Application&lt;/span&gt; &lt;span class="err"&gt;key&lt;/span&gt; &lt;span class="err"&gt;must&lt;/span&gt; &lt;span class="err"&gt;be&lt;/span&gt; &lt;span class="err"&gt;set',&lt;/span&gt;
            &lt;span class="err"&gt;'app.debug'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;'Debug&lt;/span&gt; &lt;span class="err"&gt;mode&lt;/span&gt; &lt;span class="err"&gt;must&lt;/span&gt; &lt;span class="err"&gt;be&lt;/span&gt; &lt;span class="err"&gt;disabled&lt;/span&gt; &lt;span class="err"&gt;in&lt;/span&gt; &lt;span class="err"&gt;production',&lt;/span&gt;
            &lt;span class="err"&gt;'session.secure'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;'Secure&lt;/span&gt; &lt;span class="err"&gt;cookies&lt;/span&gt; &lt;span class="err"&gt;must&lt;/span&gt; &lt;span class="err"&gt;be&lt;/span&gt; &lt;span class="err"&gt;enabled',&lt;/span&gt;
            &lt;span class="err"&gt;'session.http_only'&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;'HTTP-only&lt;/span&gt; &lt;span class="err"&gt;cookies&lt;/span&gt; &lt;span class="err"&gt;must&lt;/span&gt; &lt;span class="err"&gt;be&lt;/span&gt; &lt;span class="err"&gt;enabled',&lt;/span&gt;
        &lt;span class="err"&gt;];&lt;/span&gt;

        &lt;span class="err"&gt;foreach&lt;/span&gt; &lt;span class="err"&gt;($requiredConfigs&lt;/span&gt; &lt;span class="err"&gt;as&lt;/span&gt; &lt;span class="err"&gt;$config&lt;/span&gt; &lt;span class="err"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="err"&gt;$message)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
            &lt;span class="err"&gt;if&lt;/span&gt; &lt;span class="err"&gt;($config&lt;/span&gt; &lt;span class="err"&gt;===&lt;/span&gt; &lt;span class="err"&gt;'app.debug'&lt;/span&gt; &lt;span class="err"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="py"&gt;Config&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;===&lt;/span&gt; &lt;span class="n"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="n"&gt;RuntimeException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="nt"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;config&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;'app.debug'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nt"&gt;Config&lt;/span&gt;&lt;span class="nd"&gt;::get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nt"&gt;config&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="err"&gt;throw&lt;/span&gt; &lt;span class="err"&gt;new&lt;/span&gt; &lt;span class="err"&gt;\RuntimeException($message);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="err"&gt;}&lt;/span&gt;
    &lt;span class="err"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;In 2025, securing your Laravel app isn’t optional. it’s a continuous responsibility. From API rate limiting to CSRF protection and HTTPS enforcement, Laravel gives you everything to build secure applications. But&amp;nbsp;&lt;strong&gt;you&lt;/strong&gt; must actively implement these practices.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Want more Laravel tips?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Visit &lt;a href="https://laraveldailytips.com/" rel="noopener noreferrer"&gt;&lt;strong&gt;LaravelDailyTips&lt;/strong&gt;&lt;/a&gt; for practical guides, &lt;a href="http://laraveldailytips.com/" rel="noopener noreferrer"&gt;interview questions&lt;/a&gt;, and tricks.&lt;/p&gt;

&lt;p&gt;Subscribe now and get &lt;strong&gt;battle-tested Laravel insights&lt;/strong&gt; delivered to your inbox before anyone else!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
