<?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>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>
