<?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: MUHAMMAD Roman Ali</title>
    <description>The latest articles on DEV Community by MUHAMMAD Roman Ali (@roman_web_dev).</description>
    <link>https://dev.to/roman_web_dev</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%2F4211092%2F132fe436-cde4-455d-9b7a-30060c793176.jpg</url>
      <title>DEV Community: MUHAMMAD Roman Ali</title>
      <link>https://dev.to/roman_web_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/roman_web_dev"/>
    <language>en</language>
    <item>
      <title>Laravel Mail Not Sending? 8 Fixes That Actually Work</title>
      <dc:creator>MUHAMMAD Roman Ali</dc:creator>
      <pubDate>Sun, 08 Nov 2026 09:55:05 +0000</pubDate>
      <link>https://dev.to/roman_web_dev/laravel-mail-not-sending-8-fixes-that-actually-work-2i84</link>
      <guid>https://dev.to/roman_web_dev/laravel-mail-not-sending-8-fixes-that-actually-work-2i84</guid>
      <description>&lt;h1&gt;
  
  
  Laravel Mail Not Sending? 8 Fixes That Actually Work
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Meta description:&lt;/strong&gt; Laravel mail not sending? Follow these 8 simple fixes for SMTP, .env, cache, queue and Gmail problems. Step-by-step guide with code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;URL slug:&lt;/strong&gt; laravel-mail-not-sending&lt;/p&gt;




&lt;p&gt;If your &lt;strong&gt;Laravel mail is not sending&lt;/strong&gt;, you are not alone. It is one of the most common problems in Laravel projects. The code looks right, there is no clear error, but the email never arrives.&lt;/p&gt;

&lt;p&gt;The good news is that the cause is usually small. In this guide, you will check the most common causes in order, from the easiest to the hardest. Most people fix the problem in the first three steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick Checklist
&lt;/h2&gt;

&lt;p&gt;Before you read the details, check these first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is your &lt;code&gt;.env&lt;/code&gt; file correct?&lt;/li&gt;
&lt;li&gt;Did you clear the config cache?&lt;/li&gt;
&lt;li&gt;Are your port and encryption matching?&lt;/li&gt;
&lt;li&gt;Are you using a queue without running the worker?&lt;/li&gt;
&lt;li&gt;Are you using a Gmail normal password instead of an App Password?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's fix each one.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Check Your .env Mail Settings
&lt;/h2&gt;

&lt;p&gt;Open your &lt;code&gt;.env&lt;/code&gt; file and check these values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MAIL_MAILER=smtp
MAIL_HOST=smtp.example.com
MAIL_PORT=587
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="${APP_NAME}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common mistakes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A wrong host name, or a typo in the username.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MAIL_MAILER&lt;/code&gt; is set to &lt;code&gt;log&lt;/code&gt; or &lt;code&gt;array&lt;/code&gt;. These do not send real emails.&lt;/li&gt;
&lt;li&gt;Your password has special characters like &lt;code&gt;#&lt;/code&gt; or a space. Put the password inside quotes: &lt;code&gt;MAIL_PASSWORD="my#pass word"&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Clear the Config Cache
&lt;/h2&gt;

&lt;p&gt;Laravel can keep old settings in its cache. If you changed &lt;code&gt;.env&lt;/code&gt; but nothing changed, run:&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 config:clear
php artisan cache:clear
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your site is in production and you use &lt;code&gt;config:cache&lt;/code&gt;, run this after clearing:&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 config:cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the number one reason why a correct &lt;code&gt;.env&lt;/code&gt; still does not work.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Match the Port and Encryption
&lt;/h2&gt;

&lt;p&gt;Your port and encryption must match your mail provider. These are the common pairs:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Port&lt;/th&gt;
&lt;th&gt;Encryption&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;587&lt;/td&gt;
&lt;td&gt;TLS&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;465&lt;/td&gt;
&lt;td&gt;SSL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;25&lt;/td&gt;
&lt;td&gt;None (often blocked)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you use the wrong pair, the connection fails or times out. Check your provider's documentation. Also check your &lt;code&gt;config/mail.php&lt;/code&gt; file, because newer Laravel versions may use slightly different setting names.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Use a Gmail App Password
&lt;/h2&gt;

&lt;p&gt;If you use Gmail SMTP, your normal Gmail password will not work. Google blocks it.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Turn on 2-Step Verification in your Google account.&lt;/li&gt;
&lt;li&gt;Create an &lt;strong&gt;App Password&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Put the App Password in &lt;code&gt;MAIL_PASSWORD&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use &lt;code&gt;smtp.gmail.com&lt;/code&gt; as the host and port &lt;code&gt;587&lt;/code&gt; with TLS.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Run the Queue Worker
&lt;/h2&gt;

&lt;p&gt;If your mail class uses &lt;code&gt;ShouldQueue&lt;/code&gt;, or you use &lt;code&gt;Mail::queue()&lt;/code&gt;, the email goes to a queue first. It will not send until a worker runs.&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 queue:work
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also check &lt;code&gt;QUEUE_CONNECTION&lt;/code&gt; in &lt;code&gt;.env&lt;/code&gt;. If it is &lt;code&gt;sync&lt;/code&gt;, mail sends right away. If it is &lt;code&gt;database&lt;/code&gt; or &lt;code&gt;redis&lt;/code&gt;, you need a running worker.&lt;/p&gt;

&lt;p&gt;On a live server, use Supervisor so the worker keeps running after you close the terminal. After you change code, restart the worker:&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 queue:restart
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6. Read the Laravel Log
&lt;/h2&gt;

&lt;p&gt;Laravel writes errors to this file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;storage/logs/laravel.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the last lines. Look for words like "Connection could not be established", "Authentication failed" or "Connection timed out". The message tells you exactly where the problem is.&lt;/p&gt;

&lt;p&gt;You can also test without sending a real email. Set &lt;code&gt;MAIL_MAILER=log&lt;/code&gt;, send a mail, and check the log file. If the email text shows there, your Laravel code is fine and the problem is your SMTP settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Test with Tinker
&lt;/h2&gt;

&lt;p&gt;Test your mail setup without writing any controller code:&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 tinker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Mail&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;raw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Test email'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$message&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'your@email.com'&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;subject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Test'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this works, your SMTP is fine. The problem is in your own mail class, your queue or your event. If it fails, read the error message. It will point to the SMTP problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Check Server Blocks, Spam and Domain Settings
&lt;/h2&gt;

&lt;p&gt;If everything above is correct, check these:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Blocked port:&lt;/strong&gt; Many shared hosts and cloud servers block port 25 or 465. Ask your host if outgoing SMTP ports are open.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Spam folder:&lt;/strong&gt; The email may be sent but sitting in spam.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;From address:&lt;/strong&gt; Use an email on your own domain. Some providers reject mail from an address they do not own.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SPF and DKIM:&lt;/strong&gt; Add these records in your domain DNS. Without them, Gmail and Outlook may reject or hide your emails.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Change only one thing at a time, then test again.&lt;/li&gt;
&lt;li&gt;Use a testing tool like Mailtrap in local development, so you never send test emails to real people.&lt;/li&gt;
&lt;li&gt;Keep your mail settings in &lt;code&gt;.env&lt;/code&gt;, never inside your code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Still Stuck?
&lt;/h2&gt;

&lt;p&gt;Some mail problems depend on your server, your hosting or your DNS, and they take time to find. If your Laravel mail is still not sending, I can check and fix it for you.&lt;/p&gt;

&lt;p&gt;I am a Laravel developer. I fix SMTP, queue, 500 error and other Laravel bugs. Send me a screenshot of your error here: &lt;a href="https://www.fiverr.com/users/romanali534" rel="noopener noreferrer"&gt;Hire me on Fiverr&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravelemail</category>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How a Laravel Global Scope Silently Leaked Data in a Queue Job</title>
      <dc:creator>MUHAMMAD Roman Ali</dc:creator>
      <pubDate>Wed, 04 Nov 2026 12:15:14 +0000</pubDate>
      <link>https://dev.to/roman_web_dev/how-a-laravel-global-scope-silently-leaked-data-in-a-queue-job-2l2l</link>
      <guid>https://dev.to/roman_web_dev/how-a-laravel-global-scope-silently-leaked-data-in-a-queue-job-2l2l</guid>
      <description>&lt;h1&gt;
  
  
  Laravel Global Scope in Queue Jobs: Why It Should Fail Closed
&lt;/h1&gt;

&lt;p&gt;A Laravel global scope can quietly leak data inside a queue job. I found this bug in a multi-branch app I built. Nothing crashed. No error showed up. The report just ran with the wrong data.&lt;/p&gt;

&lt;p&gt;In this post, I explain what went wrong and how to fix it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Setup: A Branch Scope
&lt;/h2&gt;

&lt;p&gt;My app has many branches. Each user belongs to one branch. A global scope keeps every query limited to the user's branch.&lt;/p&gt;

&lt;p&gt;Here is the first version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BranchScope&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Scope&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;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Builder&lt;/span&gt; &lt;span class="nv"&gt;$builder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$branchId&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="n"&gt;branch_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$branchId&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// no branch, so no filter&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nv"&gt;$builder&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'branch_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$branchId&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;In a web request, this works well. The user is logged in. The scope knows the branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Bug: Queue Jobs Have No Logged-In User
&lt;/h2&gt;

&lt;p&gt;A queue job runs in the background. No one is logged in. So &lt;code&gt;auth()-&amp;gt;user()&lt;/code&gt; returns &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Look at the scope again. With no branch, it returns early. It adds no filter. The query now reads rows from &lt;strong&gt;all branches&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now think about a nightly report job. It was meant to email one branch its own data. Instead, it can include every branch's data. The customer gets numbers that are not theirs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Is Dangerous: It Fails Open
&lt;/h2&gt;

&lt;p&gt;This kind of bug "fails open". When something is missing, the door opens.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The job does not throw an error.&lt;/li&gt;
&lt;li&gt;The job looks normal in the logs.&lt;/li&gt;
&lt;li&gt;Nobody notices until a customer does.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A crash is easy to see and fix. A silent leak is not.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Fix: Fail Closed
&lt;/h2&gt;

&lt;p&gt;Flip the default. If there is no branch, return no rows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BranchScope&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Scope&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;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Builder&lt;/span&gt; &lt;span class="nv"&gt;$builder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$branchId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BranchContext&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$branchId&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$builder&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;whereRaw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'1 = 0'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// no branch, no rows&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nv"&gt;$builder&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'branch_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$branchId&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;&lt;code&gt;1 = 0&lt;/code&gt; is always false. So the query returns nothing.&lt;/p&gt;

&lt;p&gt;Now a missing branch gives an empty report. You see it in testing. It never reaches a customer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pass the Branch Into the Job
&lt;/h2&gt;

&lt;p&gt;The job must carry its own branch. Do not rely on a logged-in user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SendBranchReport&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;ShouldQueue&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;Queueable&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;int&lt;/span&gt; &lt;span class="nv"&gt;$branchId&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;handle&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;BranchContext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;branchId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// build and send the report&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;BranchContext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;clear&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;Dispatch it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;SendBranchReport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$branch&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;finally&lt;/code&gt; block matters. Queue workers stay alive and run many jobs. Without &lt;code&gt;clear()&lt;/code&gt;, the next job could reuse the old branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Admins Need an Explicit Way
&lt;/h2&gt;

&lt;p&gt;Some users, like super admins, must see all branches. Do not use the default for that. Make it a clear choice:&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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;scopeWithoutBranchScope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Builder&lt;/span&gt; &lt;span class="nv"&gt;$query&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;Builder&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;$query&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;withoutGlobalScope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BranchScope&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;Now "see everything" is something you write on purpose. It is never an accident.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add a Test
&lt;/h2&gt;

&lt;p&gt;This test protects you from future mistakes:&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;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;test_job_without_branch_returns_no_rows&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Invoice&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&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="nc"&gt;BranchContext&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Invoice&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If someone flips the default back, this test fails.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;A Laravel global scope that depends on &lt;code&gt;auth()&lt;/code&gt; breaks inside queue jobs.&lt;/li&gt;
&lt;li&gt;Missing context should return no rows, not all rows.&lt;/li&gt;
&lt;li&gt;Pass the branch ID into the job directly.&lt;/li&gt;
&lt;li&gt;Always clear the context after the job ends.&lt;/li&gt;
&lt;li&gt;Use a clear method for admin access.&lt;/li&gt;
&lt;li&gt;Write a test for the "no branch" case.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A silent empty result is a small problem. A silent data leak is a big one. Always choose the small one.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks to the dev.to commenter who pointed this out. Good feedback makes better code.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>security</category>
    </item>
    <item>
      <title>Branch-Level Data Isolation in Laravel with Global Scopes (and 4 Bugs I Hit)</title>
      <dc:creator>MUHAMMAD Roman Ali</dc:creator>
      <pubDate>Wed, 28 Oct 2026 14:10:55 +0000</pubDate>
      <link>https://dev.to/roman_web_dev/branch-level-data-isolation-in-laravel-with-global-scopes-and-4-bugs-i-hit-2k8c</link>
      <guid>https://dev.to/roman_web_dev/branch-level-data-isolation-in-laravel-with-global-scopes-and-4-bugs-i-hit-2k8c</guid>
      <description>&lt;p&gt;I built a multi-branch business system in Laravel. Each branch must only see its own invoices, clients, and payments. A global scope solved that in a few lines. Then the bugs started.&lt;/p&gt;

&lt;p&gt;Here is the setup, and the four problems I had to fix.&lt;/p&gt;

&lt;h2&gt;
  
  
  The trait
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trait BelongsToBranch
{
    protected static function bootBelongsToBranch(): void
    {
        static::addGlobalScope('branch', function (Builder $query) {
            $user = auth()-&amp;gt;user();

            if (! $user || $user-&amp;gt;isAdmin()) {
                return;
            }

            $query-&amp;gt;where(
                $query-&amp;gt;getModel()-&amp;gt;getTable() . '.branch_id',
                $user-&amp;gt;branch_id
            );
        });

        static::creating(function ($model) {
            if (auth()-&amp;gt;check() &amp;amp;&amp;amp; empty($model-&amp;gt;branch_id)) {
                $model-&amp;gt;branch_id = auth()-&amp;gt;user()-&amp;gt;branch_id;
            }
        });
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add &lt;code&gt;use BelongsToBranch;&lt;/code&gt; to any model, and every query is filtered automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 1: "Column 'branch_id' is ambiguous"
&lt;/h2&gt;

&lt;p&gt;My first version used &lt;code&gt;where('branch_id', ...)&lt;/code&gt;. It broke as soon as a query joined two tables that both had &lt;code&gt;branch_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; always qualify the column with the table name, as in the trait above.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 2: Shared records disappear inside relations
&lt;/h2&gt;

&lt;p&gt;Some clients are shared across branches. When a branch user opened an invoice, &lt;code&gt;$invoice-&amp;gt;client&lt;/code&gt; returned &lt;code&gt;null&lt;/code&gt;, because the scope also ran on the related query.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; remove the scope on that specific relation:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function client()
{
    return $this-&amp;gt;belongsTo(Client::class)
        -&amp;gt;withoutGlobalScope('branch');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The hard part is consistency. If you remove it in one place but not in an eager load or a &lt;code&gt;whereHas&lt;/code&gt;, the same record shows on one page and vanishes on another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 3: Queued jobs see everything
&lt;/h2&gt;

&lt;p&gt;Jobs and commands run without a logged-in user. My scope returned early when there was no user, so a queued report job read data from &lt;strong&gt;all&lt;/strong&gt; branches.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; never rely on &lt;code&gt;auth()&lt;/code&gt; inside jobs. Pass the branch explicitly:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invoice::withoutGlobalScope('branch')
    -&amp;gt;where('branch_id', $this-&amp;gt;branchId)
    -&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Bug 4: Unique validation ignores the scope
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Rule::unique('invoices', 'number')&lt;/code&gt; queries the table directly. Global scopes don't apply, so invoice number &lt;code&gt;INV-001&lt;/code&gt; in branch A blocked &lt;code&gt;INV-001&lt;/code&gt; in branch B.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; add the branch condition yourself:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rule::unique('invoices', 'number')
    -&amp;gt;where('branch_id', auth()-&amp;gt;user()-&amp;gt;branch_id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The same applies to raw &lt;code&gt;DB::table()&lt;/code&gt; queries. Global scopes only work through Eloquent.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaway
&lt;/h2&gt;

&lt;p&gt;Global scopes are a clean way to isolate data, but they work silently. Test three cases every time: a branch user, an admin, and a queued job.&lt;/p&gt;

&lt;p&gt;I build multi-branch and SaaS systems in Laravel. If you need one, you can find me here: [YOUR ADMIN PANEL / SAAS GIG LINK]&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>php</category>
      <category>saas</category>
    </item>
    <item>
      <title>Laravel Images Not Showing on cPanel? Fix It in 2 Minutes</title>
      <dc:creator>MUHAMMAD Roman Ali</dc:creator>
      <pubDate>Tue, 27 Oct 2026 14:46:00 +0000</pubDate>
      <link>https://dev.to/roman_web_dev/laravel-images-not-showing-on-cpanel-fix-it-in-2-minutes-4je</link>
      <guid>https://dev.to/roman_web_dev/laravel-images-not-showing-on-cpanel-fix-it-in-2-minutes-4je</guid>
      <description>&lt;p&gt;You uploaded your Laravel project to cPanel. Everything works, but the uploaded images don't show. You only see broken image icons.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why it happens
&lt;/h2&gt;

&lt;p&gt;Laravel saves uploaded files in &lt;code&gt;storage/app/public&lt;/code&gt;. Browsers can only see them through a link called &lt;code&gt;public/storage&lt;/code&gt;. That link doesn't upload with your files, so it doesn't exist on the server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 1: With Terminal
&lt;/h2&gt;

&lt;p&gt;Open &lt;strong&gt;Terminal&lt;/strong&gt; in cPanel and run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~/your-project
php artisan storage:link
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Refresh your site. Images should now load.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix 2: Without Terminal
&lt;/h2&gt;

&lt;p&gt;Add this route to &lt;code&gt;routes/web.php&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::get('/make-link', function () {
    Illuminate\Support\Facades\Artisan::call('storage:link');
    return 'Done';
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Open &lt;code&gt;yourdomain.com/make-link&lt;/code&gt; once. Then &lt;strong&gt;delete the route&lt;/strong&gt; so nobody else can use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Still not working?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Check that &lt;code&gt;APP_URL&lt;/code&gt; in &lt;code&gt;.env&lt;/code&gt; is your real domain, like &lt;code&gt;https://yourdomain.com&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Some hosts block links. Ask support to allow symlinks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Need help with your Laravel project on cPanel? I fix deployment issues here: &lt;a href="https://www.fiverr.com/s/YLLm23N" rel="noopener noreferrer"&gt;https://www.fiverr.com/s/YLLm23N&lt;/a&gt;&lt;/p&gt;

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