<?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: Aqib Javaid</title>
    <description>The latest articles on DEV Community by Aqib Javaid (@aqib_javaid).</description>
    <link>https://dev.to/aqib_javaid</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%2F4145615%2Fbcfee141-e994-4c88-9b3b-0cde889296b2.jpg</url>
      <title>DEV Community: Aqib Javaid</title>
      <link>https://dev.to/aqib_javaid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aqib_javaid"/>
    <language>en</language>
    <item>
      <title>Laravel Scheduler Running Twice: Fix Duplicate Runs</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Tue, 24 Nov 2026 10:49:38 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/laravel-scheduler-running-twice-fix-duplicate-runs-2fn4</link>
      <guid>https://dev.to/aqib_javaid/laravel-scheduler-running-twice-fix-duplicate-runs-2fn4</guid>
      <description>&lt;p&gt;A scheduled Laravel command that touches money, notifications or customer state has one job: do its work once. The scary part is that when it fails to do that, nothing crashes. The log is clean, the queue is healthy, and three customers get the same push notification. If your &lt;strong&gt;Laravel scheduler is running twice&lt;/strong&gt;, or two runs overlap, you usually find out from a user, not from monitoring.&lt;/p&gt;

&lt;p&gt;I hit this design question on the &lt;a href="https://aqibjavaid.site/work/ion-dash-logistics-delivery-fleet-management" rel="noopener noreferrer"&gt;ION DASH logistics platform&lt;/a&gt;, where the Laravel backend uses time-based rules: an order that sits unassigned for 15 minutes gets auto-assigned to an available driver, and a delivery waiting for customer confirmation is auto-completed after 15 minutes of silence. Both rules run from the scheduler, and both touch real people. This article covers what I did about duplicate and overlapping runs, a small test I ran for this write-up with real numbers, and where the built-in guards stop helping. If you want a senior Laravel engineer to review a scheduler that matters, my &lt;a href="https://aqibjavaid.site/services/laravel-php-development" rel="noopener noreferrer"&gt;Laravel development service&lt;/a&gt; covers exactly this kind of backend work.&lt;/p&gt;

&lt;h2&gt;
  
  
  The three ways one schedule fires more than once
&lt;/h2&gt;

&lt;p&gt;Most "scheduler ran twice" bugs come from one of three causes, and each needs a different fix. Mixing them up is why the first fix people try often does nothing.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Several servers, one cron.&lt;/strong&gt; You scaled the app to two or three web servers, each with the &lt;code&gt;* * * * * php artisan schedule:run&lt;/code&gt; entry. Every minute, every server decides the task is due. This is the classic one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A slow run overlapping the next tick.&lt;/strong&gt; The scheduler fires every minute. If your command takes 90 seconds, minute two starts while minute one is still looping over the same rows. One server is enough to cause it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overlap during a deploy.&lt;/strong&gt; A new release starts while the old process is still finishing a long command. For a few seconds two code versions run the same job. I cover the deploy side in my &lt;a href="https://aqibjavaid.site/blog/laravel-zero-downtime-deployments" rel="noopener noreferrer"&gt;zero-downtime deployments article&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Laravel gives you one guard for the first, &lt;code&gt;onOneServer()&lt;/code&gt;, and one for the second, &lt;code&gt;withoutOverlapping()&lt;/code&gt;. Neither protects you from the third, and neither is a substitute for making the work itself safe to repeat.&lt;/p&gt;

&lt;h2&gt;
  
  
  I tested what duplicate runs actually cost
&lt;/h2&gt;

&lt;p&gt;I wanted a number, not a warning. For this article I built a throwaway test in plain PHP 8.3 with SQLite in WAL mode, so treat it as a controlled demonstration of the pattern, not a production benchmark. The setup mirrors the auto-assign rule:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;500 &lt;code&gt;pending&lt;/code&gt; orders, all older than the 15-minute cutoff.&lt;/li&gt;
&lt;li&gt;Three workers started at the same moment, standing in for three servers or three overlapping ticks.&lt;/li&gt;
&lt;li&gt;A 2 ms sleep per order to stand in for the notification call that follows an assignment.&lt;/li&gt;
&lt;li&gt;A counter table that records one row per notification sent.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I ran two versions of the worker. The &lt;strong&gt;naive&lt;/strong&gt; one reads all pending orders, then loops: send, then update the status. The &lt;strong&gt;claim&lt;/strong&gt; one loops over the same candidate list but first runs &lt;code&gt;UPDATE orders SET status = 'assigned' WHERE id = ? AND status = 'pending'&lt;/code&gt; and only sends if one row was affected.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Version&lt;/th&gt;
&lt;th&gt;Workers&lt;/th&gt;
&lt;th&gt;Orders&lt;/th&gt;
&lt;th&gt;Notifications sent&lt;/th&gt;
&lt;th&gt;Duplicates&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read, send, then update&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;500&lt;/td&gt;
&lt;td&gt;1,496&lt;/td&gt;
&lt;td&gt;996&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Claim first, then send&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;500&lt;/td&gt;
&lt;td&gt;500&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The naive version sent almost exactly three notifications per order, because all three workers read the same list before any of them wrote. The claim version sent exactly 500. The only difference was one &lt;code&gt;AND status = 'pending'&lt;/code&gt; in a &lt;code&gt;WHERE&lt;/code&gt; clause and an affected-row check. That tiny pattern did more than any scheduler flag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the work idempotent first, then add the scheduler guards
&lt;/h2&gt;

&lt;p&gt;This is the order I follow now. The scheduler guards reduce how often duplicates are &lt;em&gt;attempted&lt;/em&gt;. The claim makes them &lt;em&gt;harmless&lt;/em&gt; when they happen anyway, which they will, because locks expire, caches get flushed and servers get restarted mid-run.&lt;/p&gt;

&lt;p&gt;Here is the shape of the auto-assign command in a Laravel 11+ app. The schedule lives in &lt;code&gt;routes/console.php&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="nc"&gt;Schedule&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'orders:auto-assign'&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;everyMinute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;onOneServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;withoutOverlapping&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="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;'orders-auto-assign'&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;And the command claims each row before it does anything with side effects:&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="nf"&gt;handle&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="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'pending'&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;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'created_at'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'&amp;lt;='&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;subMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;orderBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;chunkById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$orders&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$orders&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nv"&gt;$driver&lt;/span&gt; &lt;span class="o"&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;dispatcher&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;nextAvailableDriver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nv"&gt;$driver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;

                &lt;span class="c1"&gt;// The claim: only one process can flip pending -&amp;gt; assigned&lt;/span&gt;
                &lt;span class="nv"&gt;$claimed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&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;'status'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'pending'&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;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'assigned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'driver_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$driver&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="p"&gt;)&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;$claimed&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;NotifyDriverOfAssignment&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;$order&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$driver&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="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;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="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SUCCESS&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;Two details matter. The notification is dispatched as a queued job &lt;em&gt;after&lt;/em&gt; the claim succeeds, so a slow push provider cannot stretch the window in which another run might pick the same order. And the job receives IDs, not stale models, so it re-reads current state when it runs. The same "let the database pick one winner" idea is the core of my &lt;a href="https://aqibjavaid.site/blog/laravel-race-condition-double-accept-fix" rel="noopener noreferrer"&gt;race condition fix for double-accepted deliveries&lt;/a&gt;, and the dispatch rules for drivers (capacity limits, online status) sit in one place so the scheduled job and a manual accept follow identical rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each guard really protects you from
&lt;/h2&gt;

&lt;p&gt;The flags are easy to misread, so here is what the &lt;a href="https://laravel.com/docs/scheduling" rel="noopener noreferrer"&gt;Laravel scheduling documentation&lt;/a&gt; promises, next to the gaps I plan around.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Guard&lt;/th&gt;
&lt;th&gt;Stops&lt;/th&gt;
&lt;th&gt;Does not stop&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onOneServer()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Several servers all running the same due task in the same minute&lt;/td&gt;
&lt;td&gt;Overlap between two minutes on one server; any server outside the shared cache&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;withoutOverlapping()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A new tick starting while the previous run of that task is still going&lt;/td&gt;
&lt;td&gt;Runs on other servers unless the lock store is shared; a lock that outlived a crash&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;runInBackground()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A slow task blocking &lt;em&gt;other&lt;/em&gt; tasks in the same tick&lt;/td&gt;
&lt;td&gt;Duplicates of the task itself&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A conditional &lt;code&gt;UPDATE&lt;/code&gt; claim&lt;/td&gt;
&lt;td&gt;Two processes acting on the same row, whatever caused the duplicate&lt;/td&gt;
&lt;td&gt;Nothing in this list, which is why it is the one I never skip&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Some specifics that bite in practice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;onOneServer()&lt;/code&gt; needs a &lt;strong&gt;shared cache store&lt;/strong&gt; such as Redis, Memcached, DynamoDB or the database cache driver, and every server must point at the same one. A per-server &lt;code&gt;file&lt;/code&gt; cache silently gives each server its own lock.&lt;/li&gt;
&lt;li&gt;Closures and queued jobs need an explicit &lt;code&gt;-&amp;gt;name()&lt;/code&gt; to be locked reliably, which is why the example above names the task.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;withoutOverlapping()&lt;/code&gt; uses a lock that expires after &lt;strong&gt;1,440 minutes (24 hours)&lt;/strong&gt; by default. I confirmed this in the framework source (&lt;code&gt;ManagesAttributes&lt;/code&gt;, &lt;code&gt;$expiresAt = 1440&lt;/code&gt;). If the process is killed hard and the lock is not released, that task can silently stop running for a full day. Pass a sensible expiry, such as 10 minutes for a command that should take 20 seconds, and use &lt;code&gt;php artisan schedule:clear-cache&lt;/code&gt; to clear stuck mutexes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When the lock itself becomes the problem
&lt;/h2&gt;

&lt;p&gt;A lock that is too long can be worse than a duplicate. After a crash mid-run, &lt;code&gt;withoutOverlapping()&lt;/code&gt; with the default expiry means the 15-minute auto-complete rule simply does not fire for hours. Customers whose deliveries should have closed stay stuck in "awaiting confirmation". That is a quiet failure, and it is the reason I set expiries by measuring the task's normal runtime and multiplying by about five.&lt;/p&gt;

&lt;p&gt;I also add a heartbeat. Each scheduled command writes a timestamp to the cache when it finishes, and a separate check alerts if the timestamp is older than three ticks. A schedule that stops running is harder to notice than one that runs twice, so it deserves its own alarm. The &lt;a href="https://laravel.com/docs/queues" rel="noopener noreferrer"&gt;Laravel queue documentation&lt;/a&gt; describes the matching pattern for jobs (unique jobs and overlap middleware) if you push the heavy work to workers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time-based rules should be queries, not timers
&lt;/h2&gt;

&lt;p&gt;The 15-minute rules above are not implemented as "set a timer when the order is created". They are implemented as a query over state: &lt;code&gt;status = 'pending' AND created_at &amp;lt;= now() - 15 minutes&lt;/code&gt;. That choice removes a whole class of bugs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Restarts are safe.&lt;/strong&gt; If the server is down for 12 minutes, the next run simply finds more eligible rows. A timer-per-order design would have lost those timers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Late runs catch up.&lt;/strong&gt; A tick that fires 40 seconds late changes nothing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cancelled work drops out naturally.&lt;/strong&gt; If the customer cancels, the row no longer matches &lt;code&gt;pending&lt;/code&gt;, so no timer needs cancelling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It is testable.&lt;/strong&gt; Travel time with &lt;code&gt;$this-&amp;gt;travel(16)-&amp;gt;minutes()&lt;/code&gt;, run the command, assert the state. No real waiting.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cost is precision. A query-based rule fires within a minute of the deadline, not on the exact second. For dispatch and delivery confirmation that is fine. For "charge the card at exactly 09:00:00" it is not, and you would dispatch a delayed job instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  When not to use the scheduler at all
&lt;/h2&gt;

&lt;p&gt;The scheduler is a good fit for housekeeping: expiring things, retrying stragglers, nightly reports. I would move a task out of it when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The deadline is per-record and exact.&lt;/strong&gt; Dispatch a job with &lt;code&gt;-&amp;gt;delay()&lt;/code&gt; at creation time, and have the job re-check state when it wakes up.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The work is large.&lt;/strong&gt; Let the scheduler enqueue small jobs and let queue workers do the heavy lifting, so a long run cannot overlap the next tick. The &lt;a href="https://aqibjavaid.site/blog/laravel-bulk-csv-processing-queue" rel="noopener noreferrer"&gt;bulk CSV processing write-up&lt;/a&gt; shows that split on a platform where each row spends money.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You need many rows locked across several workers.&lt;/strong&gt; On MySQL 8 and PostgreSQL, &lt;code&gt;SELECT ... FOR UPDATE SKIP LOCKED&lt;/code&gt; lets workers take disjoint batches. The &lt;a href="https://dev.mysql.com/doc/refman/8.4/en/innodb-locking-reads.html" rel="noopener noreferrer"&gt;MySQL locking reads documentation&lt;/a&gt; explains the behaviour. For a few hundred rows a minute, the conditional update above is simpler and enough.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would not add &lt;code&gt;SKIP LOCKED&lt;/code&gt; to an app that processes 500 rows a minute. It earns its place at tens of thousands.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why does my Laravel scheduler run twice?
&lt;/h3&gt;

&lt;p&gt;The usual causes are several servers each running &lt;code&gt;schedule:run&lt;/code&gt;, a task that takes longer than a minute and overlaps the next tick, or a deploy that briefly runs two releases. Check how many cron entries and servers exist, then check task durations against the one-minute tick.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does onOneServer prevent duplicate runs on a single server?
&lt;/h3&gt;

&lt;p&gt;No. It only coordinates between servers using a shared cache lock. For overlap on one server use &lt;code&gt;withoutOverlapping()&lt;/code&gt;, and for real safety make the command's database writes conditional so a repeat is harmless.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I fix a scheduled task that stopped running?
&lt;/h3&gt;

&lt;p&gt;Suspect a stuck &lt;code&gt;withoutOverlapping()&lt;/code&gt; mutex first. Run &lt;code&gt;php artisan schedule:clear-cache&lt;/code&gt; and consider a shorter expiry. Then add a heartbeat alert so you notice the next time within minutes instead of days.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should scheduled tasks send notifications directly?
&lt;/h3&gt;

&lt;p&gt;I avoid it. Claim the row in the database, then dispatch a queued job for the notification. That keeps the scheduled run short and lets the queue handle retries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Duplicate scheduler runs come from multiple servers, overlapping ticks or deploys, and each has a different guard.&lt;/li&gt;
&lt;li&gt;In my 500-order test, three concurrent workers sent 1,496 notifications without a claim and exactly 500 with one.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onOneServer()&lt;/code&gt; needs a shared cache store, and &lt;code&gt;withoutOverlapping()&lt;/code&gt; defaults to a 24-hour lock, so set an expiry.&lt;/li&gt;
&lt;li&gt;Write time-based rules as queries over state, not timers, and make every side effect depend on a successful conditional update.&lt;/li&gt;
&lt;li&gt;Alert on a schedule that stops, not only on one that doubles.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have a Laravel backend where timed automation touches payments, notifications or deliveries, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; and I will review the schedule with you. Teams that also need the infrastructure side covered can look at my &lt;a href="https://aqibjavaid.site/services/cloud-devops-deployment" rel="noopener noreferrer"&gt;cloud and deployment service&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/laravel-scheduler-running-twice-fix" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>backend</category>
      <category>devops</category>
    </item>
    <item>
      <title>Laravel FCM HTTP v1: Fix Push Notifications That Never Arrive</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Fri, 20 Nov 2026 10:40:04 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/laravel-fcm-http-v1-fix-push-notifications-that-never-arrive-1lll</link>
      <guid>https://dev.to/aqib_javaid/laravel-fcm-http-v1-fix-push-notifications-that-never-arrive-1lll</guid>
      <description>&lt;p&gt;Push notifications are the feature nobody notices until they stop working. On a delivery platform I rebuilt, drivers were missing job assignments and customers were missing "your quote is ready" alerts, and the symptom was always the same: the backend logged a successful send and the phone stayed silent. If you are searching for why your &lt;strong&gt;Laravel FCM HTTP v1&lt;/strong&gt; pushes never arrive, the honest answer is that there is rarely one cause. There were three separate ones on this project, and fixing only the loudest would have left the platform half-broken.&lt;/p&gt;

&lt;p&gt;This article walks through what I found on the &lt;a href="https://aqibjavaid.site/work/ion-dash-logistics-delivery-fleet-management" rel="noopener noreferrer"&gt;ION DASH logistics and fleet platform&lt;/a&gt;, the Laravel code I ended up with, and the places where I deliberately did not use a package. It is the kind of API work I do under &lt;a href="https://aqibjavaid.site/services/api-development-integrations" rel="noopener noreferrer"&gt;API development and integrations&lt;/a&gt;, where "the call returned 200" is the start of the investigation, not the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three failures that looked like one bug
&lt;/h2&gt;

&lt;p&gt;ION DASH was a legacy delivery app being rebuilt as Flutter customer and driver apps on a Laravel API. The old notification flow was unreliable on modern Android devices. When I traced it, "unreliable" was hiding three independent problems:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;What was wrong&lt;/th&gt;
&lt;th&gt;How it showed up&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Backend to Google&lt;/td&gt;
&lt;td&gt;The notification pipeline had to move to the FCM HTTP v1 API with service-account authentication&lt;/td&gt;
&lt;td&gt;Sends relied on an older integration path Google has been retiring&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Payload&lt;/td&gt;
&lt;td&gt;Messages needed Android-safe payloads: a channel, explicit priority, string-only data values&lt;/td&gt;
&lt;td&gt;Payloads not tailored to Android could be dropped, delayed or deprioritised&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;The device&lt;/td&gt;
&lt;td&gt;Android 13+ treats notifications as a runtime permission, and the app was not asking for it&lt;/td&gt;
&lt;td&gt;Backend and Google both succeeded, the user saw nothing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The third one is the trap. No amount of server-side work fixes it, and no server-side log tells you it is happening. Google's own documentation states that Android 13 introduced the &lt;code&gt;POST_NOTIFICATIONS&lt;/code&gt; runtime permission, and that until the user grants it your app cannot show most notifications. I had to fix it in the Flutter app by requesting the permission in context, and by creating proper notification channels, before any backend change mattered.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; If a push is accepted by FCM but never appears, check the device before you check your server. On Android 13 and later, a missing notification permission looks exactly like a backend bug.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Moving to the HTTP v1 API with a service account
&lt;/h2&gt;

&lt;p&gt;The legacy FCM HTTP API authenticated with a static server key. The current API is different in two ways that matter to a Laravel developer: the endpoint is project-scoped (&lt;code&gt;POST https://fcm.googleapis.com/v1/projects/{project-id}/messages:send&lt;/code&gt;), and every request carries a short-lived OAuth 2.0 access token minted from a service-account key, with the scope &lt;code&gt;https://www.googleapis.com/auth/firebase.messaging&lt;/code&gt;, as described in &lt;a href="https://firebase.google.com/docs/cloud-messaging/send/v1-api" rel="noopener noreferrer"&gt;Google's HTTP v1 send documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That short-lived token is the part people get wrong. The access token lasts about an hour. If you mint a new one for every notification you add a round trip to Google's auth server in front of every push, and you will feel it the first time you notify a few hundred drivers at once. Cache it.&lt;/p&gt;

&lt;p&gt;The code below is a simplified version of the pattern I used (not client code). I wrapped the token in a small class instead of pulling in a full notification package:&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="o"&gt;&amp;lt;?&lt;/span&gt;&lt;span class="n"&gt;php&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;\&lt;span class="nc"&gt;Services&lt;/span&gt;\&lt;span class="nc"&gt;Push&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Google&lt;/span&gt;\&lt;span class="nc"&gt;Auth&lt;/span&gt;\&lt;span class="nc"&gt;Credentials&lt;/span&gt;\&lt;span class="nc"&gt;ServiceAccountCredentials&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate&lt;/span&gt;\&lt;span class="nc"&gt;Support&lt;/span&gt;\&lt;span class="nc"&gt;Facades&lt;/span&gt;\&lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FcmAccessToken&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;SCOPE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'https://www.googleapis.com/auth/firebase.messaging'&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="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Token is valid for ~1 hour; refresh a little early.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'fcm.access_token'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;addMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;)&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="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$credentials&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;ServiceAccountCredentials&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="kt"&gt;self&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SCOPE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nb"&gt;json_decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;file_get_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'services.fcm.credentials_path'&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="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$credentials&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetchAuthToken&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="s1"&gt;'access_token'&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;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;Two details I would not skip. First, the credentials file path lives in config and the file itself is outside the web root and outside the repository; it is a secret with project-wide send rights. Second, the cache key must live in a shared store (Redis or database), not the &lt;code&gt;array&lt;/code&gt; driver, or every queue worker will mint its own token. If you are deciding on a cache layer, my &lt;a href="https://aqibjavaid.site/blog/laravel-redis-caching-architecture" rel="noopener noreferrer"&gt;Redis caching architecture article&lt;/a&gt; covers the trade-offs.&lt;/p&gt;

&lt;h2&gt;
  
  
  A payload Android will actually honour
&lt;/h2&gt;

&lt;p&gt;With HTTP v1, a message is a JSON envelope with a target (a device token, a topic or a condition) and platform-specific blocks. The shape I settled on for driver and customer alerts looked 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="nv"&gt;$payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'message'&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;'token'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$device&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;fcm_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'notification'&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;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&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;$body&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="c1"&gt;// FCM requires every data value to be a string.&lt;/span&gt;
        &lt;span class="s1"&gt;'data'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;array_map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'strval'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'type'&lt;/span&gt;            &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'delivery_assigned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'delivery_id'&lt;/span&gt;     &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$delivery&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;'notification_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$record&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="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'android'&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;'priority'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'HIGH'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'notification'&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;'channel_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'dispatch_alerts'&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;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;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three choices are doing real work here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;channel_id&lt;/code&gt;&lt;/strong&gt; must match a channel the app created. On Android 8 and later, a notification sent to a channel that does not exist can be dropped or land in a default channel the user has muted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;priority: HIGH&lt;/code&gt;&lt;/strong&gt; is reserved for things that are genuinely time-sensitive. A driver assignment qualifies. A marketing nudge does not, and abusing high priority tends to get you deprioritised.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;notification_id&lt;/code&gt; inside &lt;code&gt;data&lt;/code&gt;&lt;/strong&gt; ties the push back to a row in my database, which becomes important in the next section.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cast &lt;code&gt;data&lt;/code&gt; values with &lt;code&gt;strval&lt;/code&gt;. FCM expects string values in &lt;code&gt;data&lt;/code&gt;, and a non-string value can come back as &lt;code&gt;INVALID_ARGUMENT&lt;/code&gt;, the same error code you can get for a bad token, so it is easy to misdiagnose.&lt;/p&gt;

&lt;h2&gt;
  
  
  Persist first, send second, treat errors as data
&lt;/h2&gt;

&lt;p&gt;The most useful decision on this project was boring: every notification is saved to a &lt;code&gt;notifications&lt;/code&gt; table before anything is sent, and the send happens in a queued job.&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;class&lt;/span&gt; &lt;span class="nc"&gt;SendPushNotification&lt;/span&gt; &lt;span class="k"&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="k"&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;InteractsWithQueue&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="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$tries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&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;array&lt;/span&gt; &lt;span class="nv"&gt;$backoff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&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="nf"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$recordId&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="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FcmClient&lt;/span&gt; &lt;span class="nv"&gt;$fcm&lt;/span&gt;&lt;span class="p"&gt;)&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;$record&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PushRecord&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;'device'&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;findOrFail&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;recordId&lt;/span&gt;&lt;span class="p"&gt;)&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;$record&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;sent_at&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;// retry after success must not double-send&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$fcm&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$record&lt;/span&gt;&lt;span class="p"&gt;)&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;$result&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;isUnregistered&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;span class="nv"&gt;$record&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;delete&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="c1"&gt;// dead token, stop sending to it&lt;/span&gt;
            &lt;span class="nv"&gt;$record&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'token_invalid'&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;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;$result&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;throwIfRetryable&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="c1"&gt;// 429 / 5xx, let the queue retry&lt;/span&gt;

        &lt;span class="nv"&gt;$record&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'sent'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'sent_at'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;now&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;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;This gives you three things a fire-and-forget &lt;code&gt;Http::post&lt;/code&gt; never does:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;An audit trail.&lt;/strong&gt; When a customer says "I never got the quote alert", I can see whether it was created, sent, rejected or never attempted. That conversation used to be a guessing game.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Idempotent retries.&lt;/strong&gt; The &lt;code&gt;sent_at&lt;/code&gt; guard means a retried job does not buzz a driver's phone twice. The same thinking applies to any external call, and I go deeper on it in &lt;a href="https://aqibjavaid.site/blog/laravel-api-idempotency-keys" rel="noopener noreferrer"&gt;API idempotency keys in Laravel&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean token hygiene.&lt;/strong&gt; With HTTP v1, a registration that no longer exists returns &lt;code&gt;UNREGISTERED&lt;/code&gt; (HTTP 404). Google's &lt;a href="https://firebase.google.com/docs/cloud-messaging/manage-tokens" rel="noopener noreferrer"&gt;token management guidance&lt;/a&gt; says to delete those registrations, and it notes that on Android a registration inactive for 270 days is considered expired and rejected. If you never prune, you keep paying a request for every dead device, and your delivery numbers look worse than they are.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The retry classification matters. A 404 &lt;code&gt;UNREGISTERED&lt;/code&gt; is permanent, so retrying it is wasted work. A 429 or 5xx is transient, so it should go back on the queue with backoff. If you want the broader pattern for jobs that call flaky third parties, see &lt;a href="https://aqibjavaid.site/blog/laravel-queue-architecture-saas" rel="noopener noreferrer"&gt;how I structure Laravel queues for SaaS&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing so it stays fixed
&lt;/h2&gt;

&lt;p&gt;Notification bugs are the sort that return months later when someone changes a payload helper. I added regression tests for the cases that had already bitten us, using &lt;code&gt;Http::fake()&lt;/code&gt; so no real call leaves the test suite:&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;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'deletes the device token when FCM says it is unregistered'&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="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'fcm.googleapis.com/*'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Http&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'error'&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;'details'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'errorCode'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'UNREGISTERED'&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;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;404&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;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nv"&gt;$record&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PushRecord&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="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="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;new&lt;/span&gt; &lt;span class="nc"&gt;SendPushNotification&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$record&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="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FcmClient&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&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;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$record&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;device&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;exists&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toBeFalse&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;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 regression tests I keep for this cover: payload data values are all strings, the Android channel and priority are present, an unregistered token is removed, a transient failure is retried, and a second run after success sends nothing. The Laravel &lt;a href="https://laravel.com/docs/12.x/http-client" rel="noopener noreferrer"&gt;HTTP client fake&lt;/a&gt; makes each of these a few lines. None of these tests can prove the phone shows the notification, which is why the manual device checklist below still exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would do differently, and when not to hand-roll this
&lt;/h2&gt;

&lt;p&gt;I wrote the client myself because I needed control over persistence, retries and token cleanup, and because the integration is small: one token class, one sender, one job. That is a trade-off, not a rule.&lt;/p&gt;

&lt;p&gt;Use a maintained package or the Firebase Admin SDK if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you only need basic sends and have no need for a delivery audit table;&lt;/li&gt;
&lt;li&gt;your team will not maintain the code after you leave;&lt;/li&gt;
&lt;li&gt;you also send to iOS or web and want one abstraction across platforms.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Hand-rolling is justified when push is part of your business workflow, as it was for dispatch, where a missed assignment is a missed delivery, and when you want failures to be visible data rather than a log line.&lt;/p&gt;

&lt;p&gt;What I would change on a fresh build: record the device's reported notification permission state at registration time. The ION DASH fix was found by testing on real Android 13 devices. If the app reported "permission denied" to the API, the support team could see it immediately instead of discovering it through complaints.&lt;/p&gt;

&lt;p&gt;A manual checklist I now run on every mobile project that sends push:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Fresh install on an Android 13+ device: is the permission prompt shown in context, and does a push arrive after granting it?&lt;/li&gt;
&lt;li&gt;[ ] Deny the permission, then trigger a push: does the backend record the send as accepted while the user sees nothing (expected), and does the app offer a way to re-enable?&lt;/li&gt;
&lt;li&gt;[ ] App swiped away and in the background: does a high-priority push still arrive?&lt;/li&gt;
&lt;li&gt;[ ] Reinstall the app: is the old token cleaned up after the first &lt;code&gt;UNREGISTERED&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;[ ] Revoke the service account key in a staging project: do failures surface in logs and alerts rather than silently?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why does FCM return success but the notification never appears?
&lt;/h3&gt;

&lt;p&gt;A successful response means FCM accepted the message, not that the device displayed it. The usual causes are a missing &lt;code&gt;POST_NOTIFICATIONS&lt;/code&gt; permission on Android 13+, a notification channel the user muted or the app never created, or an app-level setting that suppresses foreground display.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need the Firebase Admin SDK to use HTTP v1 from Laravel?
&lt;/h3&gt;

&lt;p&gt;No. HTTP v1 is a plain REST API. You need a service-account credential to mint an OAuth access token and an HTTP client to call &lt;code&gt;messages:send&lt;/code&gt;. A package is a convenience, not a requirement.&lt;/p&gt;

&lt;h3&gt;
  
  
  How often should I refresh the FCM access token?
&lt;/h3&gt;

&lt;p&gt;The token is valid for roughly an hour, so I cache it for about 50 minutes in a shared cache store. Minting one per message adds latency and load for no benefit.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does INVALID_ARGUMENT mean in a v1 response?
&lt;/h3&gt;

&lt;p&gt;It can mean a malformed token or a malformed payload. Check the payload first (for example non-string &lt;code&gt;data&lt;/code&gt; values), then the token. Google's documentation says it can signal either.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A silent push usually has more than one cause: the API, the payload and the device each need checking.&lt;/li&gt;
&lt;li&gt;Android 13+ needs a runtime notification permission. No backend change can substitute for it.&lt;/li&gt;
&lt;li&gt;Cache the service-account access token in a shared store instead of minting one per send.&lt;/li&gt;
&lt;li&gt;Persist notifications before sending, send from a queue, and make retries idempotent.&lt;/li&gt;
&lt;li&gt;Treat &lt;code&gt;UNREGISTERED&lt;/code&gt; as a signal to delete the token, and 429/5xx as a signal to retry.&lt;/li&gt;
&lt;li&gt;Hand-roll the client only when push is part of your business workflow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your app has notifications that are accepted but never seen, I am happy to look at it. You can &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; and we will go through the send path together.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/laravel-fcm-http-v1-push-notifications" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>firebase</category>
      <category>android</category>
      <category>php</category>
    </item>
    <item>
      <title>Laravel Race Condition Fix: Stop Double-Accepted Jobs</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Tue, 17 Nov 2026 10:40:05 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/laravel-race-condition-fix-stop-double-accepted-jobs-3bo8</link>
      <guid>https://dev.to/aqib_javaid/laravel-race-condition-fix-stop-double-accepted-jobs-3bo8</guid>
      <description>&lt;p&gt;Two drivers tap &lt;strong&gt;Accept&lt;/strong&gt; on the same delivery within the same second. Both phones show a green "Job accepted" screen. Both drivers start driving. One of them arrives to find the job already gone, or worse, the customer has two vehicles outside. This is the classic Laravel race condition, and if you run any dispatch, booking or claim-the-job workflow, you will meet it the first week you have real traffic.&lt;/p&gt;

&lt;p&gt;I hit exactly this shape of problem while rebuilding the dispatch layer on &lt;a href="https://aqibjavaid.site/work/ion-dash-logistics-delivery-fleet-management" rel="noopener noreferrer"&gt;ION DASH, a logistics and fleet platform&lt;/a&gt; (Flutter apps for customers and drivers on a Laravel API). The rule the client wanted was simple: the first driver who successfully accepts a delivery secures it, and nobody else can. Making that rule true under concurrency took more care than the feature name suggests. This article is the version of that fix I would hand to another &lt;a href="https://aqibjavaid.site/services/laravel-php-development" rel="noopener noreferrer"&gt;Laravel developer&lt;/a&gt;: why the obvious code fails, four ways to fix it, which one I reach for first, and how to prove it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug report that starts it
&lt;/h2&gt;

&lt;p&gt;The symptom is never "race condition". It is a support message: &lt;em&gt;"Two drivers say they got the same order."&lt;/em&gt; Nothing in your logs is red, because each request succeeded.&lt;/p&gt;

&lt;p&gt;Here is the code that produces it. It reads like correct code, and in a single-user test it is correct:&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="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&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="nc"&gt;Delivery&lt;/span&gt; &lt;span class="nv"&gt;$delivery&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$driver&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;user&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$delivery&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Already taken'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;409&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$delivery&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'status'&lt;/span&gt;    &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'assigned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'driver_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$driver&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="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;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$delivery&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fresh&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;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bug is the gap between the &lt;code&gt;if&lt;/code&gt; and the &lt;code&gt;update&lt;/code&gt;. This is a check-then-act sequence, and the database can serve two requests inside that gap:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Time&lt;/th&gt;
&lt;th&gt;Driver A's request&lt;/th&gt;
&lt;th&gt;Driver B's request&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;t0&lt;/td&gt;
&lt;td&gt;reads delivery, status = &lt;code&gt;pending&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t1&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;reads delivery, status = &lt;code&gt;pending&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t2&lt;/td&gt;
&lt;td&gt;passes the check, writes &lt;code&gt;driver_id = A&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;t3&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;passes its (stale) check, writes &lt;code&gt;driver_id = B&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;B's write silently overwrites A's. Both got a 200. The window between t0 and t3 is only milliseconds wide, but a busy dispatch queue, a push notification that wakes many phones at once, and flaky mobile networks that retry requests all push traffic into that window. On a &lt;a href="https://aqibjavaid.site/services/mobile-app-development" rel="noopener noreferrer"&gt;mobile-backed product&lt;/a&gt; it happens far more often than you expect, because every driver receives the same "new job" push at the same moment.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Important&lt;/p&gt;
&lt;p&gt;
You cannot fix this in PHP with an &lt;code&gt;if&lt;/code&gt;. PHP processes (FPM workers, queue workers, Octane workers) don't share memory. Only the database, or a shared lock service, can decide who wins.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Four ways to fix it, and what each costs
&lt;/h2&gt;

&lt;p&gt;There are four patterns I consider. They are not equal, and the right one depends on how much state the decision touches.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;How it works&lt;/th&gt;
&lt;th&gt;Strength&lt;/th&gt;
&lt;th&gt;Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Conditional &lt;code&gt;UPDATE&lt;/code&gt; (compare-and-set)&lt;/td&gt;
&lt;td&gt;One statement: &lt;code&gt;UPDATE ... WHERE status = 'pending'&lt;/code&gt;, check affected rows&lt;/td&gt;
&lt;td&gt;Simplest, no explicit locks, fast&lt;/td&gt;
&lt;td&gt;Only guards one row's state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pessimistic lock (&lt;code&gt;lockForUpdate&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Lock the row inside a transaction, then run any checks&lt;/td&gt;
&lt;td&gt;Lets you validate several things before writing&lt;/td&gt;
&lt;td&gt;Holds a row lock; needs a transaction; can deadlock if misused&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Atomic cache lock (&lt;code&gt;Cache::lock&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Redis-backed lock keyed by delivery id&lt;/td&gt;
&lt;td&gt;Works across tables and services&lt;/td&gt;
&lt;td&gt;A second system to fail; lock can expire mid-work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Optimistic version column&lt;/td&gt;
&lt;td&gt;Write only if &lt;code&gt;version&lt;/code&gt; matches what you read&lt;/td&gt;
&lt;td&gt;No locks held&lt;/td&gt;
&lt;td&gt;Callers must retry; more code&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;I'll go through them in the order I would try them.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Conditional update: my default
&lt;/h3&gt;

&lt;p&gt;The cleanest fix removes the gap entirely. Don't read, then write. Write with the condition inside the same statement and let the database tell you whether you won:&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;$won&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Delivery&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;whereKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$delivery&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="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;'status'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'pending'&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;whereNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'driver_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;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'status'&lt;/span&gt;      &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'assigned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'driver_id'&lt;/span&gt;   &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$driver&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;'assigned_at'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;now&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;span class="p"&gt;)&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;$won&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Already taken'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;409&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;update()&lt;/code&gt; returns the number of rows changed. InnoDB takes a row lock while it evaluates and applies that single statement, so when two requests collide, one updates the row and the second sees &lt;code&gt;status = 'assigned'&lt;/code&gt; in its &lt;code&gt;WHERE&lt;/code&gt;, matches zero rows and returns &lt;code&gt;0&lt;/code&gt;. The decision and the write are one atomic step. In the ION DASH flow, this is the shape of "first driver to accept secures the job": the loser gets a clean 409 and the app shows "this job was just taken", instead of a false success.&lt;/p&gt;

&lt;p&gt;Use this whenever the winning condition is a property of one row. It is also the pattern I'd suggest for the claim step of any job board, booking slot or inventory reservation.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Pessimistic locking: when you must check more than one thing
&lt;/h3&gt;

&lt;p&gt;Dispatch is rarely just "is the row pending". In our case the backend also enforces a &lt;strong&gt;driver capacity limit&lt;/strong&gt; (a driver cannot hold more assignments than their configured maximum) and &lt;strong&gt;online/offline eligibility&lt;/strong&gt;. Those rules live on other rows, so a single-row conditional update is not enough.&lt;/p&gt;

&lt;p&gt;That is where &lt;a href="https://laravel.com/docs/12.x/queries" rel="noopener noreferrer"&gt;&lt;code&gt;lockForUpdate&lt;/code&gt;&lt;/a&gt; earns its place. Lock the rows you are about to reason about, inside a transaction, in a fixed order:&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="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$deliveryId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$driverId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Lock the driver first, then the delivery. Always in this order.&lt;/span&gt;
    &lt;span class="nv"&gt;$driver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Driver&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$driverId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lockForUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nv"&gt;$driver&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;is_online&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="s1"&gt;'ok'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'reason'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'offline'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Delivery&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'driver_id'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$driver&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;whereIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'assigned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'picked_up'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nb"&gt;count&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$active&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nv"&gt;$driver&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;max_active_jobs&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="s1"&gt;'ok'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'reason'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'at_capacity'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$delivery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Delivery&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$deliveryId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lockForUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$delivery&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'ok'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'reason'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'taken'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$delivery&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'assigned'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'driver_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$driver&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="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="s1"&gt;'ok'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="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;Locking the &lt;strong&gt;driver&lt;/strong&gt; row is the detail people miss. If you lock only the delivery, the same driver can accept two different jobs at the same moment, both pass the capacity count, and now they are one over their limit. Locking the driver serialises that driver's own accepts.&lt;/p&gt;

&lt;p&gt;Two rules keep this safe:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Always lock in the same order&lt;/strong&gt; (driver, then delivery). Two transactions taking the same locks in opposite orders is how you get deadlocks. Laravel retries the closure for you if you pass a second argument: &lt;code&gt;DB::transaction($callback, 3)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the transaction short.&lt;/strong&gt; No HTTP calls, no push notifications and no file uploads inside it. Send the Firebase push after the commit, for example with &lt;code&gt;DB::afterCommit()&lt;/code&gt; or a queued job dispatched &lt;code&gt;-&amp;gt;afterCommit()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;MySQL's own documentation on &lt;a href="https://dev.mysql.com/doc/refman/8.4/en/innodb-locking-reads.html" rel="noopener noreferrer"&gt;locking reads&lt;/a&gt; is worth ten minutes, particularly the parts on which rows get locked when your &lt;code&gt;WHERE&lt;/code&gt; clause doesn't hit an index. An unindexed lock query can lock far more rows than you intended.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Atomic cache locks: for work that spans systems
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://laravel.com/docs/12.x/cache" rel="noopener noreferrer"&gt;&lt;code&gt;Cache::lock()&lt;/code&gt;&lt;/a&gt; gives you a Redis-backed mutex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cache&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"delivery-accept:{$delivery-&amp;gt;id}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="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="o"&gt;!&lt;/span&gt; &lt;span class="nv"&gt;$lock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'message'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Busy, try again'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;409&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;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ... decide and write ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$lock&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;release&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use this when the thing being protected is not a single database row: a call to an external carrier API, a document being generated, a report being rebuilt. For dispatch I avoid it as the primary guard, for a reason that matters: &lt;strong&gt;the lock and the data live in different systems&lt;/strong&gt;. If the lock expires after 10 seconds while a slow request is still working, a second request walks in. The database lock cannot expire out from under the transaction that holds it. Treat a cache lock as a traffic cop in front of the real guard, not the guard itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Optimistic versioning: for edit conflicts, not races to claim
&lt;/h3&gt;

&lt;p&gt;Add a &lt;code&gt;version&lt;/code&gt; integer, read it, and write with &lt;code&gt;WHERE version = :seen&lt;/code&gt;, incrementing on success. It's the right tool when many people might edit the same record over minutes (an admin editing a quote, say) and you would rather show a "someone else changed this" message than block anyone. For a first-come-first-served claim, the conditional update does the same job with less code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Auto-assignment makes the race worse
&lt;/h2&gt;

&lt;p&gt;Accept-by-tap is one writer. The other writer is your own scheduler. On ION DASH, an order that stays in the queue for 15 minutes is automatically assigned to an available driver. That means a driver tapping &lt;strong&gt;Accept&lt;/strong&gt; and the scheduled auto-assign command can target the same delivery at the same instant.&lt;/p&gt;

&lt;p&gt;The fix is the same principle applied to both paths: they must go through &lt;strong&gt;one&lt;/strong&gt; method that performs the guarded write, rather than each carrying its own copy of the rules. When I see two code paths that both set &lt;code&gt;driver_id&lt;/code&gt;, I treat that as a bug waiting to happen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="c1"&gt;// One door in. The controller and the scheduled command both call this.&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClaimDelivery&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="nf"&gt;__invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$deliveryId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$driverId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// conditional update + capacity checks live here, nowhere else&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;Also stop the scheduler overlapping itself. If the command takes longer than its interval, two copies run at once, which is another source of double assignment. Laravel's &lt;a href="https://laravel.com/docs/12.x/scheduling" rel="noopener noreferrer"&gt;&lt;code&gt;withoutOverlapping()&lt;/code&gt;&lt;/a&gt; uses an atomic lock for exactly this, and &lt;code&gt;onOneServer()&lt;/code&gt; covers multi-server deployments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test it with real concurrency
&lt;/h2&gt;

&lt;p&gt;A feature test that calls the endpoint twice in a row will pass against the broken code, because the second call runs after the first has finished. To expose a race you need two database connections acting at the same time.&lt;/p&gt;

&lt;p&gt;My checklist when I review claim logic:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[x] The check and the write are one statement, or one transaction holding a lock&lt;/li&gt;
&lt;li&gt;[x] Losing the race returns a clear 409 the mobile app can show&lt;/li&gt;
&lt;li&gt;[x] Every path that assigns a driver goes through the same method&lt;/li&gt;
&lt;li&gt;[x] Locks are always taken in the same order&lt;/li&gt;
&lt;li&gt;[x] No network calls inside a transaction&lt;/li&gt;
&lt;li&gt;[x] A concurrency test fires 20 or more simultaneous accepts at one delivery and asserts exactly one winner&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For that last item, a small script is enough. Run it against a real MySQL database, not SQLite, because SQLite serialises all writes and hides the problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"20 parallel accepts"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;2
seq 1 20 &lt;span class="o"&gt;|&lt;/span&gt; xargs -P 20 -I&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt; curl -s -o /dev/null -w &lt;span class="s2"&gt;"%{http_code}\n"&lt;/span&gt; \
  -X POST https://staging.example.test/api/deliveries/42/accept \
  -H &lt;span class="s2"&gt;"Authorization: Bearer $(cat tokens/driver-{}.txt)"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You expect exactly one &lt;code&gt;200&lt;/code&gt; and nineteen &lt;code&gt;409&lt;/code&gt; responses. Against the check-then-update version, you can see more than one &lt;code&gt;200&lt;/code&gt;, and an &lt;code&gt;assigned&lt;/code&gt; row whose &lt;code&gt;driver_id&lt;/code&gt; is whichever request wrote last.&lt;/p&gt;

&lt;h2&gt;
  
  
  When not to over-engineer this
&lt;/h2&gt;

&lt;p&gt;Not every status change needs a lock. I skip all of this when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;There is only ever one writer.&lt;/strong&gt; A background job that owns a record doesn't need to protect it from itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The operation is naturally idempotent.&lt;/strong&gt; Marking an already-delivered order as delivered again is harmless. (Retries of &lt;em&gt;payments&lt;/em&gt; are different, and need &lt;a href="https://aqibjavaid.site/blog/laravel-api-idempotency-keys" rel="noopener noreferrer"&gt;idempotency keys&lt;/a&gt;.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Traffic is tiny.&lt;/strong&gt; An internal tool with three users can live with a &lt;code&gt;409&lt;/code&gt; from a unique constraint.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A database &lt;strong&gt;unique index&lt;/strong&gt; is also worth knowing as a backstop. If a business rule says "one active assignment per delivery", enforce it with a unique key on a nullable &lt;code&gt;active_driver_slot&lt;/code&gt; or a similar column, so that even a bug in your application code cannot create a double assignment. The code is your first defence and the schema is your last.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd do again, and what I'd change
&lt;/h2&gt;

&lt;p&gt;What worked on ION DASH: one claim method, conditional update first, row locks only where the capacity rules needed them, and pushes sent after commit. Losing drivers get an immediate, explicit "already taken" response instead of a ghost job.&lt;/p&gt;

&lt;p&gt;What I'd watch for next time: lock-wait timeouts under heavy load. If many drivers fight for the same hot row, MySQL's &lt;code&gt;SKIP LOCKED&lt;/code&gt; (available in 8.0+) lets workers skip rows that are already locked, which suits a queue where any available job will do. It doesn't suit a case where drivers want one specific job, but it is the right tool for pulling "the next available" item. Queue workers raise the same class of problem, and I covered the design side in the &lt;a href="https://aqibjavaid.site/blog/laravel-queue-architecture-saas" rel="noopener noreferrer"&gt;Laravel queue architecture guide&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Is &lt;code&gt;lockForUpdate&lt;/code&gt; enough to prevent double booking in Laravel?
&lt;/h3&gt;

&lt;p&gt;Only inside a database transaction. Outside one, the lock is released the moment the statement ends. Wrap the read and the write together in &lt;code&gt;DB::transaction()&lt;/code&gt;, and lock every row whose state your decision depends on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I use Redis locks or database locks for first-come-first-served claims?
&lt;/h3&gt;

&lt;p&gt;Prefer the database when the thing being claimed is a row. The lock lives with the data and can't expire mid-decision. Use Redis locks to protect work that spans several systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why does my concurrency test pass on my machine but fail in production?
&lt;/h3&gt;

&lt;p&gt;Local tests often run on SQLite or run requests one at a time. Test against MySQL or PostgreSQL with real parallel connections.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does this apply to mobile app backends specifically?
&lt;/h3&gt;

&lt;p&gt;It applies to any API, but mobile backends hit it sooner. A single push notification can wake hundreds of devices at once, and flaky networks make clients retry requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Read-then-write is a race. Move the condition into the &lt;code&gt;UPDATE&lt;/code&gt;'s &lt;code&gt;WHERE&lt;/code&gt; clause and check the affected-row count.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;lockForUpdate&lt;/code&gt; inside a short transaction when the decision involves more than one row, and lock in a consistent order.&lt;/li&gt;
&lt;li&gt;Put one claim method behind every code path, including scheduled auto-assignment.&lt;/li&gt;
&lt;li&gt;Redis locks are a traffic cop, not the source of truth. A unique index is your last line of defence.&lt;/li&gt;
&lt;li&gt;Prove it with 20 parallel requests against MySQL, expecting exactly one winner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are building dispatch, booking or marketplace logic and want it reviewed by someone who has shipped it, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; and we can look at your claim flow together.&lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/laravel-race-condition-double-accept-fix" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>mysql</category>
      <category>backend</category>
    </item>
    <item>
      <title>Migrate a WordPress Theme to Plain PHP: A Real Case</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Fri, 13 Nov 2026 10:39:34 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/migrate-a-wordpress-theme-to-plain-php-a-real-case-15jk</link>
      <guid>https://dev.to/aqib_javaid/migrate-a-wordpress-theme-to-plain-php-a-real-case-15jk</guid>
      <description>&lt;p&gt;A WordPress site that has outgrown its theme usually gets one of two verdicts: patch it forever, or rewrite it in something modern. Both are expensive guesses made before anyone has measured the problem. When I was asked to move a Miami tattoo studio off WordPress, I did the measuring first, and the number I found changed the whole plan. If you are weighing &lt;strong&gt;how to migrate a WordPress theme to plain PHP&lt;/strong&gt;, or whether to leave WordPress at all, this is the method, with the trade-offs.&lt;/p&gt;

&lt;p&gt;The short version: the theme was huge, but the part of it that actually depended on WordPress was tiny. Finding that out took an afternoon and saved weeks.&lt;/p&gt;

&lt;h2&gt;
  
  
  The number that changed the plan
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://aqibjavaid.site/work/tattoo-panda-wordpress-to-plain-php-contentful" rel="noopener noreferrer"&gt;Tattoo Panda rebuild&lt;/a&gt; started from a site built as a custom child theme on top of a commercial parent theme. It carried thousands of lines of CSS, a 2,500-line home page template and a multi-step booking modal. The studio's owners are artists, and they wanted the design, the animation and the behaviour exactly as they were, on shared hosting where PHP is the only runtime and nothing can be compiled.&lt;/p&gt;

&lt;p&gt;Everyone's instinct at this size is "rewrite it". Before agreeing, I catalogued every call the theme made into WordPress. Across every template there were only about 15 distinct WordPress functions, and almost all page content was hardcoded HTML rather than coming from the database.&lt;/p&gt;

&lt;p&gt;You can do the same audit in minutes. List every function call in the theme, then check each one against the WordPress function reference:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# distinct function calls across the theme, most frequent first&lt;/span&gt;
grep -rhoE &lt;span class="s1"&gt;'\b[a-z_]+\('&lt;/span&gt; wp-content/themes/my-child-theme --include=*.php \
  &lt;span class="o"&gt;|&lt;/span&gt; sort &lt;span class="o"&gt;|&lt;/span&gt; uniq -c &lt;span class="o"&gt;|&lt;/span&gt; sort -rn &lt;span class="o"&gt;|&lt;/span&gt; head -80

&lt;span class="c"&gt;# then keep only the ones WordPress defines: get_header, wp_head,&lt;/span&gt;
&lt;span class="c"&gt;# esc_html, home_url, admin-ajax handlers, and so on&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output is noisy, but the shape of it is not. A theme that calls &lt;code&gt;wp_query&lt;/code&gt; and custom post types in every template is a CMS in disguise, and you should keep a CMS. A theme that mostly echoes hardcoded markup and calls a handful of helpers is a set of PHP files wearing a WordPress costume.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note&lt;/p&gt;
&lt;p&gt;
The size of a theme tells you nothing about how much of it depends on WordPress. Count the dependencies, not the lines.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why I chose plain PHP, and what I rejected
&lt;/h2&gt;

&lt;p&gt;With only about 15 functions to replace, the options changed. This is the comparison I made, and the reason each one lost or won:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Option&lt;/th&gt;
&lt;th&gt;What it would have cost&lt;/th&gt;
&lt;th&gt;Verdict&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rewrite in a PHP framework&lt;/td&gt;
&lt;td&gt;Re-learning the design in new templates, new routing, new tooling, for no new capability&lt;/td&gt;
&lt;td&gt;Rejected: risk with no benefit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Astro or other static generator&lt;/td&gt;
&lt;td&gt;A second runtime and a build toolchain the shared host cannot run&lt;/td&gt;
&lt;td&gt;Rejected: booking and voting need a server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Static HTML&lt;/td&gt;
&lt;td&gt;The header, footer and booking modal pasted into every page&lt;/td&gt;
&lt;td&gt;Rejected: maintenance trap&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Plain PHP with Contentful for content&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Mechanical removal of WordPress calls from templates that were already PHP&lt;/td&gt;
&lt;td&gt;Chosen&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The deciding fact was hosting. The site lives on SiteGround shared hosting, where PHP is the only runtime. Booking, voting and writing to Contentful all need server code, and PHP was already there. The result has no Composer packages, no npm dependencies and no build step, and it deploys over SFTP. If you are weighing this against a full framework or against Next.js, my comparison of &lt;a href="https://aqibjavaid.site/blog/nextjs-vs-wordpress-for-business" rel="noopener noreferrer"&gt;Next.js and WordPress for a business site&lt;/a&gt; covers the cases where a heavier stack is the right call. Plain PHP wins only when the existing templates are already PHP and the host constrains you.&lt;/p&gt;

&lt;p&gt;If you want a second opinion on a migration like this, the &lt;a href="https://aqibjavaid.site/services/wordpress-woocommerce-development" rel="noopener noreferrer"&gt;WordPress and WooCommerce development service&lt;/a&gt; page explains how I scope them.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make the live site the spec
&lt;/h2&gt;

&lt;p&gt;"Does it look the same?" is not something you can answer by eye across nine pages, and it is the question the client cares about most. So I turned it into a test.&lt;/p&gt;

&lt;p&gt;Before changing anything, I captured the rendered HTML of all nine pages and both theme stylesheets as a read-only baseline. Then I wrote a script, &lt;code&gt;verify.sh&lt;/code&gt;, that renders every page of the new site locally and compares it with its capture. For each page it reports four things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;word-level text differences&lt;/li&gt;
&lt;li&gt;CSS class differences&lt;/li&gt;
&lt;li&gt;PHP warnings&lt;/li&gt;
&lt;li&gt;leftover URLs pointing at the old domain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It exits non-zero if any page drifts. The idea, as a sketch rather than the exact script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/usr/bin/env bash&lt;/span&gt;
&lt;span class="c"&gt;# Compare each locally rendered page with its captured WordPress baseline.&lt;/span&gt;
&lt;span class="nv"&gt;fail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0
&lt;span class="k"&gt;for&lt;/span&gt; page &lt;span class="k"&gt;in&lt;/span&gt; home gallery gear contact giveaway booking tattoo-fight privacy terms&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  curl -s &lt;span class="s2"&gt;"http://localhost:8080/$page"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /tmp/new-&lt;span class="nv"&gt;$page&lt;/span&gt;.html
  &lt;span class="c"&gt;# strip volatile bits (cache-busting query strings, nonces) before diffing&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; ! diff &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;normalise baseline/&lt;span class="nv"&gt;$page&lt;/span&gt;.html&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;normalise /tmp/new-&lt;span class="nv"&gt;$page&lt;/span&gt;.html&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
    &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"DRIFT: $page"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nv"&gt;fail&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1
  &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;span class="nb"&gt;exit&lt;/span&gt; &lt;span class="nv"&gt;$fail&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All nine pages were diffed against their originals. The harness is cheap to build, and it turns an argument about taste into a red or green result. It is the part of this project I would reuse on any "identical to the original" job, including ones that move to a different stack. It also keeps you honest about the CSS cascade: the two theme stylesheets stay separate and load in their original order, because merging them changes specificity and breaks things quietly. Each template's large inline style block moved verbatim into its own page stylesheet, with no reformatting or minifying.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep old endpoints alive with shims
&lt;/h2&gt;

&lt;p&gt;The most useful trick in the migration is also the least glamorous. The contest page's JavaScript posted to WordPress's &lt;code&gt;admin-ajax.php&lt;/code&gt;. I could have rewritten that front-end code. Instead I routed the old path to a small PHP dispatcher that sends each action to its new handler, using Apache rewrites (&lt;a href="https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html" rel="noopener noreferrer"&gt;mod_rewrite docs&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight apache"&gt;&lt;code&gt;&lt;span class="nc"&gt;RewriteEngine&lt;/span&gt; &lt;span class="nc"&gt;On&lt;/span&gt;
&lt;span class="c1"&gt;# old WordPress AJAX entry point keeps working
&lt;/span&gt;&lt;span class="nc"&gt;RewriteRule&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt;&lt;span class="n"&gt;wp&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ajax&lt;/span&gt;\&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;php&lt;/span&gt;$ &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;php&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;L&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="no"&gt;QSA&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# old giveaway path redirects permanently
&lt;/span&gt;&lt;span class="na"&gt;RewriteRule ^give/?$ /giveaway [R&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;301&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="no"&gt;L&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 code carried over unchanged while the server side was replaced underneath it. The same idea covers the contact form and booking endpoints. The same rules also give pretty URLs, a custom 404 and a redirect from the old &lt;code&gt;/give/&lt;/code&gt; path, and keep internal folders away from the web.&lt;/p&gt;

&lt;p&gt;If your site also has search rankings to protect, the redirect work is a bigger topic in its own right, and I walk through it in &lt;a href="https://aqibjavaid.site/blog/migrate-wordpress-to-nextjs-seo" rel="noopener noreferrer"&gt;migrating WordPress to Next.js without losing SEO&lt;/a&gt;. The principle is the same: never make the old URLs stop answering.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving content and forms to Contentful
&lt;/h2&gt;

&lt;p&gt;Artist grids on the home and gallery pages now come from Contentful, so the studio edits its roster in one place. Two decisions are worth copying.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reads go through a short cache.&lt;/strong&gt; Reads are cached for five minutes. Cache files are written to a temporary name and then renamed, so a failed write can never leave a half-written cache for the next visitor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;cache_put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;)&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;$tmp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$path&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'.'&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nf"&gt;bin2hex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;random_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&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="s1"&gt;'.tmp'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;file_put_contents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$tmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;LOCK_EX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$tmp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// atomic on the same filesystem&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Writes never trust the browser.&lt;/strong&gt; The booking form runs the full &lt;a href="https://www.contentful.com/developers/docs/references/content-management-api/" rel="noopener noreferrer"&gt;Contentful Management API&lt;/a&gt; flow on the server: upload the reference image, create and process the asset, poll until it is ready, publish it, then create and publish the appointment entry. The browser only ever sends an artist slug. The server maps that slug to the Contentful entry itself, so a tampered value is rejected with a 400, and the write token never reaches the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  The voting contest: small, but it still needed real security
&lt;/h2&gt;

&lt;p&gt;The studio runs a public voting contest, backed by three MySQL tables for contests, the leaderboard and votes. This is the part of the site where a casual rewrite would have been dangerous, so it is worth showing what I kept strict.&lt;/p&gt;

&lt;p&gt;Votes are protected by email one-time codes. A six-digit code is generated with &lt;a href="https://www.php.net/manual/en/function.random-int.php" rel="noopener noreferrer"&gt;&lt;code&gt;random_int&lt;/code&gt;&lt;/a&gt;, is valid for five minutes and is compared with &lt;a href="https://www.php.net/manual/en/function.hash-equals.php" rel="noopener noreferrer"&gt;&lt;code&gt;hash_equals&lt;/code&gt;&lt;/a&gt; so the comparison does not leak timing information. Daily per-email and per-IP limits are enforced before a vote counts. Every query uses PDO prepared statements, and the nomination form checks file type with &lt;code&gt;finfo&lt;/code&gt;, caps uploads at 5 MB and sanitises file names. If the database is unreachable, the page falls back to its empty state instead of an error.&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;$code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;str_pad&lt;/span&gt;&lt;span class="p"&gt;(&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="nf"&gt;random_int&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="mi"&gt;999999&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;STR_PAD_LEFT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// store a hash of $code with an expiry of now + 5 minutes, then email it&lt;/span&gt;
&lt;span class="c1"&gt;// on submit:&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nf"&gt;hash_equals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$storedHash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'sha256'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$submitted&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;span class="cm"&gt;/* reject */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plain PHP does not mean casual PHP. Removing a framework removes its guard rails too, so each of them has to be rebuilt on purpose.&lt;/p&gt;

&lt;h2&gt;
  
  
  When this approach is the wrong choice
&lt;/h2&gt;

&lt;p&gt;Plain PHP on shared hosting was right for this one site. It would be wrong in several other situations, and you should say no to it when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Content is the product.&lt;/strong&gt; If editors publish daily, need drafts, roles and revisions, keep WordPress or move to a proper headless CMS with a real front end.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The theme leans on plugins.&lt;/strong&gt; If the audit finds forms, memberships, ecommerce and SEO logic living in plugins, the dependency count is far above 15, and the cheaper path may be fixing the site rather than leaving it. For the slow-admin version of that problem, start with the &lt;a href="https://aqibjavaid.site/blog/woocommerce-slow-admin-fix" rel="noopener noreferrer"&gt;WooCommerce slow admin fix&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The team will grow.&lt;/strong&gt; Hand-rolled routing and no tests are fine for one maintainer and awkward for five. A framework gives newcomers a map.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;You have no baseline.&lt;/strong&gt; Without a way to diff against the original, "identical" is a hope, not a result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the bigger question of whether to replace a system or reshape it, &lt;a href="https://aqibjavaid.site/blog/rewrite-vs-refactor-legacy-application" rel="noopener noreferrer"&gt;rewrite vs refactor for a legacy application&lt;/a&gt; gives the framework I use, and the &lt;a href="https://aqibjavaid.site/services/legacy-application-modernization" rel="noopener noreferrer"&gt;legacy application modernization service&lt;/a&gt; page shows where this kind of work fits.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can I move a WordPress site to plain PHP without losing my design?
&lt;/h3&gt;

&lt;p&gt;Yes, if the theme is mostly hardcoded markup. Keep the stylesheets and their load order as they are, port templates by replacing the WordPress calls, and use a page-by-page diff against captures of the live site to prove nothing moved.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I know if a custom theme is small enough to port?
&lt;/h3&gt;

&lt;p&gt;Count the distinct WordPress functions it calls and check how much content comes from the database. Roughly 15 helpers and mostly hardcoded pages is a small port. Custom post types, widgets and plugin hooks in every template is a CMS you should keep.&lt;/p&gt;

&lt;h3&gt;
  
  
  Will old URLs and forms keep working?
&lt;/h3&gt;

&lt;p&gt;They can, if you route them on purpose. Apache rewrites can send old paths such as the admin AJAX entry point to a dispatcher, and permanent redirects cover moved pages, so existing front-end code and inbound links keep working.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is Contentful a replacement for the WordPress admin?
&lt;/h3&gt;

&lt;p&gt;For structured content like an artist roster, yes. Editors work in Contentful, and the site reads it through a short cache. It is not a drop-in for plugin-heavy features such as memberships.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Audit what the theme actually depends on before choosing a rewrite. About 15 WordPress functions, not 2,500 lines, decided the plan here.&lt;/li&gt;
&lt;li&gt;Choose the stack the host can run. Shared hosting with PHP only made plain PHP the lowest-risk option.&lt;/li&gt;
&lt;li&gt;Capture the live pages and diff against them, so "identical" is a repeatable check that exits non-zero on drift.&lt;/li&gt;
&lt;li&gt;Shim old endpoints instead of rewriting working browser code.&lt;/li&gt;
&lt;li&gt;Rebuild the security rules deliberately when you drop a framework: one-time codes, rate limits, prepared statements and server-side mapping of IDs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you have a WordPress site you suspect is smaller than it looks, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; and I will tell you honestly whether to port it, fix it or leave it alone.&lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/migrate-wordpress-theme-to-plain-php" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>php</category>
      <category>webdev</category>
      <category>migration</category>
    </item>
    <item>
      <title>Laravel Bulk CSV Processing: Queues, Holds and Retries</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Tue, 10 Nov 2026 10:39:59 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/laravel-bulk-csv-processing-queues-holds-and-retries-48hd</link>
      <guid>https://dev.to/aqib_javaid/laravel-bulk-csv-processing-queues-holds-and-retries-48hd</guid>
      <description>&lt;p&gt;A dealer uploads a CSV with 500 rows. Each row is a SIM card that has to be activated on one of several carrier APIs, and each activation costs real wholesale money that you cannot get back if the row was bad. That was the shape of the bulk activation feature I built in Laravel for a wireless dealer platform, and it is a good test of what &lt;strong&gt;Laravel bulk CSV processing with queues&lt;/strong&gt; actually requires once money and third-party APIs are involved.&lt;/p&gt;

&lt;p&gt;Most tutorials stop at "chunk the file and dispatch a job per row". That is the easy 20%. The hard 80% is deciding what happens before the first row is touched, who pays while the job runs, what a retry is allowed to repeat, and how the user finds out what happened. Below are the five decisions I made on that project, what each one prevented, and the one I would revisit today. If you are scoping a similar feature as part of an &lt;a href="https://aqibjavaid.site/services/api-development-integrations" rel="noopener noreferrer"&gt;API integration project&lt;/a&gt;, these are the places where the estimate usually moves.&lt;/p&gt;

&lt;p&gt;The client work is under NDA, so I describe patterns, not proprietary code. The full public write-up is in the &lt;a href="https://aqibjavaid.site/work/multi-carrier-bulk-sim-activation-platform" rel="noopener noreferrer"&gt;bulk SIM activation case study&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision 1: Fail the whole file before anything is charged
&lt;/h2&gt;

&lt;p&gt;Different carriers wanted different CSV shapes. Some needed an ICCID and ZIP code per row. One needed a ZIP code and a quantity, plus proof that inventory existed for that ZIP. The upload screen detects the shape from the header row, then runs a &lt;strong&gt;provider-specific preflight&lt;/strong&gt; before any money moves.&lt;/p&gt;

&lt;p&gt;The preflight for the ZIP-and-quantity carrier checks plan-code coverage for every ZIP up front. If one row cannot be fulfilled, the whole batch is rejected with a row-level error list. Nothing is held, nothing is sent.&lt;/p&gt;

&lt;p&gt;I chose all-or-nothing on purpose. The alternative is to charge, call the carrier, find out it rejects row 212, and refund. That "charge then refund" path is where accounting bugs live: partial refunds, refunds that fail, and support tickets asking where the money went. A rejected file costs the dealer thirty seconds to fix. A wrong ledger costs hours.&lt;/p&gt;

&lt;p&gt;The rule I took from it: &lt;strong&gt;validate against the upstream system's own constraints, not just your own.&lt;/strong&gt; Your CSV validator knows a ZIP code has five digits. Only the carrier knows whether it has stock there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BatchPreflight&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/** @return list&amp;lt;array{row:int, error:string}&amp;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="nf"&gt;check&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CarrierService&lt;/span&gt; &lt;span class="nv"&gt;$carrier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;iterable&lt;/span&gt; &lt;span class="nv"&gt;$rows&lt;/span&gt;&lt;span class="p"&gt;)&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="nv"&gt;$errors&lt;/span&gt; &lt;span class="o"&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;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$rows&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;)&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;$problem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$carrier&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;preflight&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nv"&gt;$errors&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="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'row'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'error'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$problem&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// +2: header + 1-based&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$errors&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;This is a simplified sketch of the idea, not the client's code. The real preflight is provider-specific, and routing it through each carrier's service class keeps the controller from branching on carrier names.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision 2: Hold the money first, settle it row by row
&lt;/h2&gt;

&lt;p&gt;Once preflight passes, the platform estimates the total cost of the batch and &lt;strong&gt;holds&lt;/strong&gt; it up the wallet chain before the batch is saved. In this platform, a distributor can fund a dealer and a dealer can fund its own store, so one batch can touch more than one balance.&lt;/p&gt;

&lt;p&gt;The hold happens in a single database transaction with the batch record. Two concurrent uploads from the same store cannot both pass a balance check, because the balance row is locked while it is read. In Laravel that is &lt;code&gt;lockForUpdate()&lt;/code&gt;, which takes an InnoDB row lock; the &lt;a href="https://dev.mysql.com/doc/refman/8.4/en/innodb-locking-reads.html" rel="noopener noreferrer"&gt;MySQL locking reads documentation&lt;/a&gt; explains exactly what is locked and for how long.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="no"&gt;DB&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$store&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$estimate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$wallet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Wallet&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;whereKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$store&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;wallet_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;lockForUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;firstOrFail&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$wallet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;available&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nv"&gt;$estimate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InsufficientFunds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$estimate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$wallet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;available&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nv"&gt;$wallet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;decrement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'available'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$estimate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$wallet&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'held'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$estimate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nv"&gt;$batch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ActivationBatch&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="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'store_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$store&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;'held'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$estimate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'total'&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="nv"&gt;$rows&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;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;createMany&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$rows&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;ProcessBatchJob&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;$batch&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;afterCommit&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;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;Note &lt;code&gt;afterCommit()&lt;/code&gt;. Without it, a fast worker can pick up the job before the transaction commits and find an empty batch. That bug only appears under load, which is why it survives local testing.&lt;/p&gt;

&lt;p&gt;As each row completes, the worker converts a slice of the hold into a real charge and releases the difference for failed rows. The tier that pays is decided per row: distributor-funded rows and self-funded store rows land on different ledgers. The point of holding up front is simple. Once the batch is accepted, the job can run for a long time and the dealer's balance is already accounted for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision 3: One long job, with a deliberately long timeout
&lt;/h2&gt;

&lt;p&gt;Each batch runs as a single queued job with a &lt;strong&gt;two-hour timeout&lt;/strong&gt;. That sounds wrong if you have read "keep jobs short" advice, and in general that advice is right. Here it was a trade-off.&lt;/p&gt;

&lt;p&gt;Some carrier sandboxes were slow and rate limited. A per-row job design would have needed a shared rate limiter across workers and a way to know when "the last row" finished. One job per batch kept ordering simple and made it easy to throttle, because one loop controls the pace of every call to that carrier.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;Good for&lt;/th&gt;
&lt;th&gt;Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;One job per batch&lt;/td&gt;
&lt;td&gt;Strict carrier rate limits, simple ordering, easy "is it done" check&lt;/td&gt;
&lt;td&gt;Long-running worker, a crash mid-batch needs resume logic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One job per row (&lt;code&gt;Bus::batch&lt;/code&gt;)&lt;/td&gt;
&lt;td&gt;Parallelism, per-row retries, fine-grained progress&lt;/td&gt;
&lt;td&gt;Needs shared rate limiting and a completion callback&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If I built it again for a carrier with generous limits, I would use &lt;a href="https://laravel.com/docs/12.x/queues#job-batching" rel="noopener noreferrer"&gt;Laravel job batches&lt;/a&gt; with a per-row job and a &lt;code&gt;RateLimited&lt;/code&gt; middleware. The framework already gives you &lt;code&gt;then&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt; and &lt;code&gt;finally&lt;/code&gt; callbacks plus progress counts, which is most of what I hand-built. For the slow, strict carriers on this project, the single long job was the more predictable choice.&lt;/p&gt;

&lt;p&gt;Whichever you pick, set &lt;code&gt;$timeout&lt;/code&gt; below the queue connection's &lt;code&gt;retry_after&lt;/code&gt;, or the queue will hand the same batch to a second worker while the first is still running. That is the classic duplicate-activation bug, and with real SIMs it costs money. The queue &lt;a href="https://laravel.com/docs/12.x/queues#job-expirations-and-timeouts" rel="noopener noreferrer"&gt;documentation on timeouts and retry_after&lt;/a&gt; covers the relationship.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision 4: Make retries repeat only what failed
&lt;/h2&gt;

&lt;p&gt;Carrier APIs fail in boring ways: timeouts, 502s, a token that expired mid-batch. The job records a status on &lt;strong&gt;every row&lt;/strong&gt; (pending, activated, failed, with the carrier's message), and the dealer sees that list.&lt;/p&gt;

&lt;p&gt;Retry is a separate action that re-submits only the failed rows. Rows already marked activated are never sent again. This is the same idea as an idempotency key, applied to your own data model. If the external API supports idempotency keys, use them as well; &lt;a href="https://docs.stripe.com/api/idempotent_requests" rel="noopener noreferrer"&gt;Stripe's idempotent requests&lt;/a&gt; are the reference design. Many carrier APIs do not, so your own row status is the only guard against activating the same SIM twice.&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="nf"&gt;retryFailed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ActivationBatch&lt;/span&gt; &lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="p"&gt;)&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;$failed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'failed'&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="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;abort_if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$failed&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;422&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Nothing to retry.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// re-hold only the cost of the failed rows, then re-queue&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;holdFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$failed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'status'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'queued'&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="nc"&gt;ProcessBatchJob&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;$batch&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;afterCommit&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;Inside the job, the loop skips anything not in &lt;code&gt;pending&lt;/code&gt;. A crash halfway through leaves a batch in an honest state: some rows activated, the rest pending, and a clear retry path. The same rule shows up in &lt;a href="https://aqibjavaid.site/blog/stripe-webhooks-laravel-verify-dedupe-retry" rel="noopener noreferrer"&gt;Stripe webhook handling&lt;/a&gt;: &lt;strong&gt;record the outcome before you acknowledge it, and make replays harmless.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Decision 5: Stream progress instead of polling for it
&lt;/h2&gt;

&lt;p&gt;A dealer watching a 500-row batch does not want a spinner for several minutes, and I did not want 500 browsers each polling every second. The batch page opens a &lt;strong&gt;Server-Sent Events&lt;/strong&gt; stream. The endpoint reads the batch record every two seconds and pushes &lt;code&gt;completed&lt;/code&gt;, &lt;code&gt;failed&lt;/code&gt; and &lt;code&gt;total&lt;/code&gt; counters, then closes when the batch finishes or fails. It also closes after a five-minute cap, so a stream can never hang around indefinitely.&lt;/p&gt;

&lt;p&gt;SSE is one-way, works over plain HTTP, and the browser's &lt;code&gt;EventSource&lt;/code&gt; reconnects for you. See the MDN guide to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events" rel="noopener noreferrer"&gt;using server-sent events&lt;/a&gt; for the wire format. For one-way progress updates it is far simpler than WebSockets; For the wider set of options, see my article on &lt;a href="https://aqibjavaid.site/blog/real-time-features-laravel-saas-broadcasting-websockets" rel="noopener noreferrer"&gt;real-time features in Laravel SaaS&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$deadline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;addMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;lt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$deadline&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;refresh&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;echo&lt;/span&gt; &lt;span class="s1"&gt;'data: '&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;only&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'completed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'total'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'status'&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="s2"&gt;"\n\n"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nd"&gt;@ob_flush&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="nf"&gt;flush&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;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;in_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$batch&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'done'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'failed'&lt;/span&gt;&lt;span class="p"&gt;]&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="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;connection_aborted&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;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="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;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Content-Type'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'text/event-stream'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Cache-Control'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'no-cache'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'X-Accel-Buffering'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'no'&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;Two operational notes. &lt;code&gt;X-Accel-Buffering: no&lt;/code&gt; stops nginx from buffering the stream until it closes. And every open stream occupies a PHP-FPM worker for its lifetime, so the five-minute cap is a safety valve, not a detail. If you expect many dealers watching at once, size the pool for it or move the stream to a dedicated process.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I would change, and when not to build this
&lt;/h2&gt;

&lt;p&gt;I would not hand-roll this for every CSV feature. A back-office import where a bad row costs nothing needs far less: validate, import in chunks, email a report. The hold, preflight and row ledger only earn their complexity when each row triggers an irreversible external action or spends money.&lt;/p&gt;

&lt;p&gt;Things I would revisit on a new build:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move to &lt;code&gt;Bus::batch&lt;/code&gt; with per-row jobs wherever the upstream rate limits allow it.&lt;/li&gt;
&lt;li&gt;Store the upload and per-row results on durable storage from day one, so support can answer "what happened to row 212" without a developer.&lt;/li&gt;
&lt;li&gt;Alert on a batch that stops making progress, not only on failures. A job stuck inside a slow call produces no error at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quick checklist before you start
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;List every external call a row triggers and which of them cannot be undone.&lt;/li&gt;
&lt;li&gt;Write the preflight rules from the upstream system's constraints, then reject the file, not the row, when money is involved.&lt;/li&gt;
&lt;li&gt;Hold funds inside the same transaction that creates the batch, and dispatch with &lt;code&gt;afterCommit()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Give every row its own status and make retries skip anything already done.&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;$timeout&lt;/code&gt; below &lt;code&gt;retry_after&lt;/code&gt; and test with a worker killed mid-batch.&lt;/li&gt;
&lt;li&gt;Decide how users see progress before you decide how the job runs.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you are planning a bulk workflow like this and want a second opinion on the design, you can &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; and I will walk through the failure cases with you.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/laravel-bulk-csv-processing-queue" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>queue</category>
      <category>api</category>
    </item>
    <item>
      <title>Marketplace Website Development Cost: What to Budget</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Wed, 04 Nov 2026 10:40:25 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/marketplace-website-development-cost-what-to-budget-3me5</link>
      <guid>https://dev.to/aqib_javaid/marketplace-website-development-cost-what-to-budget-3me5</guid>
      <description>&lt;p&gt;If you are planning a marketplace, the first question is almost always the same: what does &lt;strong&gt;marketplace website development cost&lt;/strong&gt;? The honest answer is that the price is driven by a handful of decisions that have nothing to do with how the site looks. Payments between three parties, trust and safety, search, messaging and admin tooling are where the budget goes.&lt;/p&gt;

&lt;p&gt;I'm Aqib Javaid, a senior full-stack engineer, and I've built marketplace-style platforms where money, content and multiple user roles all have to work together. This guide breaks down what actually moves the cost, gives rough planning ranges by scope, and shows where founders overspend. The ranges are planning estimates, not quotes. A real number needs a scoped spec.&lt;/p&gt;

&lt;h2&gt;
  
  
  What makes a marketplace more expensive than a normal website?
&lt;/h2&gt;

&lt;p&gt;A brochure site has one audience. A marketplace has at least three: buyers, sellers (or vendors, or service providers) and you, the operator. Every feature has to be built three times over, once for each side's view and permissions.&lt;/p&gt;

&lt;p&gt;Four things separate a marketplace from a typical web app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Split payments.&lt;/strong&gt; Money goes from buyer to seller, minus your commission, and sometimes with refunds, disputes and payouts on a schedule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Two-sided accounts.&lt;/strong&gt; Sellers need onboarding, profiles, listings and dashboards. Buyers need search, favourites, orders and messaging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trust and moderation.&lt;/strong&gt; Reviews, reporting, listing approval and admin controls are not optional once real strangers transact.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Liquidity tooling.&lt;/strong&gt; Search, filters, notifications and email flows that bring both sides back.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these are exotic, but each one adds weeks, and they interact. That is why a "simple Airbnb for X" is rarely simple.&lt;/p&gt;

&lt;h2&gt;
  
  
  How much does a marketplace cost at different scopes?
&lt;/h2&gt;

&lt;p&gt;Treat these as rough planning ranges for a senior engineer or small team building a custom product. Agency rates, location and team size will move them in either direction.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scope&lt;/th&gt;
&lt;th&gt;What is included&lt;/th&gt;
&lt;th&gt;Typical planning range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Validation MVP&lt;/td&gt;
&lt;td&gt;One listing type, simple seller profiles, basic search, one payment flow, admin panel&lt;/td&gt;
&lt;td&gt;$15,000 to $35,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Launch-ready marketplace&lt;/td&gt;
&lt;td&gt;Seller onboarding, split payments with payouts, messaging, reviews, email notifications, admin moderation&lt;/td&gt;
&lt;td&gt;$35,000 to $80,000&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scaled platform&lt;/td&gt;
&lt;td&gt;Multi-language, real-time messaging, advanced search, multiple payment methods, analytics, mobile apps&lt;/td&gt;
&lt;td&gt;$80,000 and up&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The wide spread exists because "marketplace" covers a services board and a full multi-vendor commerce platform equally. If you are still deciding what to include in version one, the same logic I use in the &lt;a href="https://aqibjavaid.site/blog/saas-mvp-development-cost" rel="noopener noreferrer"&gt;SaaS MVP development cost&lt;/a&gt; breakdown applies here: build the smallest thing that proves buyers and sellers will actually transact.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where does the money actually go?
&lt;/h2&gt;

&lt;p&gt;On a typical build, I see the budget split roughly like this: payments and payouts, seller onboarding and dashboards, search and listings, messaging and notifications, admin and moderation, then design and QA. Payments are consistently the line founders underestimate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Payments and payouts
&lt;/h3&gt;

&lt;p&gt;Taking one payment is a day of work. Taking a payment, holding the platform fee, paying the seller later, handling refunds and reconciling it all is a project. Stripe's own &lt;a href="https://docs.stripe.com/connect/marketplace" rel="noopener noreferrer"&gt;Connect marketplace documentation&lt;/a&gt; is the right place to see how fund flows work, and the choice of charge type changes who is liable for disputes and refunds. Decide this before you design the seller dashboard, because it shapes the whole data model.&lt;/p&gt;

&lt;p&gt;On the &lt;a href="https://aqibjavaid.site/work/imperial-votes-nextjs-saas-voting-platform-case-study" rel="noopener noreferrer"&gt;Imperial Votes platform&lt;/a&gt;, the payment logic had to support different countries, currencies and payment gateways, alongside organizer dashboards and paid voting. Multi-gateway support was a major part of the work, not an afterthought.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multi-language and localisation
&lt;/h3&gt;

&lt;p&gt;If your marketplace serves more than one country, translation becomes a content system, not a plugin. On &lt;a href="https://aqibjavaid.site/work/planuojam-marketplace-case-study" rel="noopener noreferrer"&gt;Planuojam, a multilingual event planning marketplace&lt;/a&gt;, we rescued a failing platform by building a headless marketplace in Next.js and Strapi 5 that supports five languages, real-time messaging and Stripe payments. We added an AWS Translate-powered engine in Strapi to automate nested content localisation while preserving SEO integrity. Strapi's &lt;a href="https://docs.strapi.io/cms/features/internationalization" rel="noopener noreferrer"&gt;internationalization docs&lt;/a&gt; cover the baseline, but anything beyond the baseline takes custom work, and it should be priced in.&lt;/p&gt;

&lt;h3&gt;
  
  
  Messaging and notifications
&lt;/h3&gt;

&lt;p&gt;Buyer-to-seller messaging looks small on a feature list. Real-time delivery, unread counts, email fallbacks and abuse controls make it a mid-sized feature. If you can launch with a simple contact form and add live chat once you have traffic, you save real money.&lt;/p&gt;

&lt;h3&gt;
  
  
  Admin and moderation
&lt;/h3&gt;

&lt;p&gt;Founders forget that they are the third user. You need to approve sellers, remove listings, handle refunds, see payouts and export reports. A weak admin panel turns every support request into a developer ticket.&lt;/p&gt;

&lt;h2&gt;
  
  
  Build custom, use a SaaS platform, or start with a plugin?
&lt;/h2&gt;

&lt;p&gt;Not every marketplace needs custom code on day one.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Approach&lt;/th&gt;
&lt;th&gt;Best for&lt;/th&gt;
&lt;th&gt;Trade-off&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Marketplace SaaS or plugin&lt;/td&gt;
&lt;td&gt;Testing demand quickly with standard flows&lt;/td&gt;
&lt;td&gt;Limited customisation, fees, and you may outgrow it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Custom build on a proven stack&lt;/td&gt;
&lt;td&gt;Unique workflows, multiple payment rules, SEO-led growth&lt;/td&gt;
&lt;td&gt;Higher upfront cost, full control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hybrid: CMS plus custom app&lt;/td&gt;
&lt;td&gt;Content-heavy marketplaces needing editorial control&lt;/td&gt;
&lt;td&gt;Needs good architecture so the pieces stay in sync&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;My rule of thumb: if your differentiator is the workflow or the pricing model, build custom. If your differentiator is supply or community, test with an off-the-shelf tool first. For the stack itself, I usually reach for &lt;a href="https://aqibjavaid.site/services/react-nextjs-development" rel="noopener noreferrer"&gt;Next.js and React&lt;/a&gt; on the front end with a Laravel or Strapi backend, depending on whether the product is application-heavy or content-heavy. The &lt;a href="https://nextjs.org/docs" rel="noopener noreferrer"&gt;Next.js documentation&lt;/a&gt; is a good starting reference for what the framework handles out of the box.&lt;/p&gt;

&lt;p&gt;If the platform will have subscriptions, tiers or multiple organisations on it, read the &lt;a href="https://aqibjavaid.site/blog/multi-tenant-saas-shared-vs-separate-database" rel="noopener noreferrer"&gt;multi-tenant database trade-offs&lt;/a&gt; before you commit to a data model. Changing it later is expensive. Our &lt;a href="https://aqibjavaid.site/services/saas-development" rel="noopener noreferrer"&gt;SaaS development service&lt;/a&gt; covers exactly this kind of architecture work.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can you reduce marketplace development cost without cutting corners?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pick one side to serve first.&lt;/strong&gt; Launch with a supply you personally curate, so you don't need full seller self-service on day one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep one payment method and one currency&lt;/strong&gt; until revenue proves you need more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use managed services&lt;/strong&gt; for email, search and file storage instead of building them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delay the mobile apps.&lt;/strong&gt; A responsive web app validates demand for a fraction of the price. The &lt;a href="https://aqibjavaid.site/blog/mobile-app-development-cost" rel="noopener noreferrer"&gt;mobile app development cost guide&lt;/a&gt; shows when native is worth it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write the spec before you hire.&lt;/strong&gt; Most budget overruns come from features added mid-build, not from the original scope.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Budget for after launch.&lt;/strong&gt; Plan for hosting, fixes and iteration from the start.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Tip&lt;/p&gt;
&lt;p&gt;
Before you ask for quotes, write one page describing who pays whom, when, and who handles a refund. That one page removes more estimate variance than any feature list.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  When should you not build a custom marketplace?
&lt;/h2&gt;

&lt;p&gt;Skip the custom build if you have no proof that sellers will list and buyers will pay. A spreadsheet, a form and manual payouts can validate demand for almost nothing. I would rather you spend $2,000 learning that the idea doesn't work than $50,000 finding out after launch.&lt;/p&gt;

&lt;p&gt;Also avoid building if a niche Shopify store or WooCommerce setup already covers your model. Our &lt;a href="https://aqibjavaid.site/blog/shopify-vs-custom-ecommerce" rel="noopener noreferrer"&gt;custom versus Shopify comparison&lt;/a&gt; explains where that line sits for commerce-style products.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How long does it take to build a marketplace?
&lt;/h3&gt;

&lt;p&gt;A focused validation MVP usually takes two to three months with a small team. A launch-ready platform with split payments, messaging and moderation more often takes four to six months. Multi-language or mobile app scope extends that.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the ongoing costs after launch?
&lt;/h3&gt;

&lt;p&gt;Expect hosting, third-party fees (payment processing, email, maps or search), security updates and a steady stream of improvements. Plan a monthly maintenance budget rather than treating launch as the finish line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I build the marketplace in phases?
&lt;/h3&gt;

&lt;p&gt;Yes, and I recommend it. Phase one proves transactions work. Phase two adds the features that raise retention, such as reviews and messaging. Phase three scales with localisation or mobile.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need a developer or an agency?
&lt;/h3&gt;

&lt;p&gt;For a focused MVP, a senior full-stack engineer is often faster and cheaper than an agency because there are fewer handoffs. Larger platforms with design, QA and project management needs may justify a team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Marketplace cost is driven by payments, two-sided accounts, moderation and admin tooling, not by page design.&lt;/li&gt;
&lt;li&gt;Planning ranges run from roughly $15,000 for a validation MVP to $80,000 and up for a scaled, multi-language platform.&lt;/li&gt;
&lt;li&gt;Decide your payment model (who holds funds, who handles refunds) before you design anything else.&lt;/li&gt;
&lt;li&gt;Validate demand manually first, then build only the side of the marketplace you need to start transacting.&lt;/li&gt;
&lt;li&gt;Write a one-page spec of the money flow before requesting quotes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are scoping a marketplace and want a realistic number, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; and I'll walk through your requirements with you.&lt;/p&gt;

&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/marketplace-website-development-cost" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>startup</category>
      <category>stripe</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>SaaS MVP Development Cost: What to Budget Before You Build</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Mon, 02 Nov 2026 14:14:16 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/saas-mvp-development-cost-what-to-budget-before-you-build-2124</link>
      <guid>https://dev.to/aqib_javaid/saas-mvp-development-cost-what-to-budget-before-you-build-2124</guid>
      <description>&lt;p&gt;Founders ask me the same question early in almost every SaaS conversation: "What will the MVP cost?" The honest answer is that &lt;strong&gt;SaaS MVP development cost&lt;/strong&gt; is mostly decided by a handful of scope choices you make before a developer writes a line of code. The tech stack matters far less than people expect.&lt;/p&gt;

&lt;p&gt;This guide walks through the pieces that make up the budget, the features that quietly inflate it, and how I help founders cut a first version down to something that proves demand. It complements my general guide to &lt;a href="https://aqibjavaid.site/blog/custom-web-app-mvp-cost-2026" rel="noopener noreferrer"&gt;custom web app MVP cost&lt;/a&gt;, with a focus on subscription software. If you are planning a build, my &lt;a href="https://aqibjavaid.site/services/saas-development" rel="noopener noreferrer"&gt;SaaS development service&lt;/a&gt; is the place to start.&lt;/p&gt;

&lt;p&gt;I am not giving fixed price figures here, because a number without a scope is meaningless. What I can give you is the structure of the estimate, so you can judge any quote you receive.&lt;/p&gt;

&lt;h2&gt;
  
  
  What an MVP is (and is not)#
&lt;/h2&gt;

&lt;p&gt;An MVP is the &lt;strong&gt;smallest product that lets real users complete the one job you are selling, so you can learn whether they will pay for it.&lt;/strong&gt; It is not a cheap version of the final product. It is a deliberately narrow product.&lt;/p&gt;

&lt;p&gt;Most overspending comes from treating the MVP as "version one of everything." Each extra feature adds build time, testing, support and a longer wait before you learn anything.&lt;/p&gt;

&lt;h2&gt;
  
  
  The building blocks of a SaaS budget#
&lt;/h2&gt;

&lt;p&gt;Every SaaS product shares a core of plumbing before your unique value appears. Know what is in it:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Block&lt;/th&gt;
&lt;th&gt;What it includes&lt;/th&gt;
&lt;th&gt;Why it costs more than it looks&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Accounts and auth&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Sign-up, login, password reset, email verification, optional social login&lt;/td&gt;
&lt;td&gt;Security details and edge cases&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Roles and permissions&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Admin, member, owner; team invites&lt;/td&gt;
&lt;td&gt;Grows with every new role&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Billing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Plans, trials, upgrades, failed payments, invoices, taxes&lt;/td&gt;
&lt;td&gt;Billing is a state machine, not a form&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Core feature&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;The one thing users came for&lt;/td&gt;
&lt;td&gt;Your actual product&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Admin tools&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Manage users, support, content&lt;/td&gt;
&lt;td&gt;Needed from day one for support&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Notifications&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Transactional email, in-app alerts&lt;/td&gt;
&lt;td&gt;Deliverability and templates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Infrastructure&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Hosting, environments, backups, monitoring&lt;/td&gt;
&lt;td&gt;Often forgotten in estimates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Design and UX&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Flows, UI, responsive layouts&lt;/td&gt;
&lt;td&gt;Quality drives conversion&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Testing and QA&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Automated tests, manual passes&lt;/td&gt;
&lt;td&gt;Prevents costly regressions&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;When you receive a quote, check that each of these is accounted for. A suspiciously low number usually leaves out billing, admin tools or testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Features that quietly inflate the cost#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Multi-tenancy#
&lt;/h3&gt;

&lt;p&gt;If each customer is a separate organisation with isolated data, you need a tenancy model from the start. It touches every query and permission check. I explain the options in &lt;a href="https://aqibjavaid.site/blog/multi-tenant-saas-shared-vs-separate-database" rel="noopener noreferrer"&gt;shared vs separate database for multi-tenant SaaS&lt;/a&gt;. Decide this early, because retrofitting it is expensive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Billing complexity#
&lt;/h3&gt;

&lt;p&gt;A single flat monthly plan is straightforward. Add usage-based pricing, multiple currencies, seat-based plans, coupons, proration and annual contracts, and the effort multiplies. Start with the simplest model that lets you charge, and let customer feedback justify more. My notes on &lt;a href="https://aqibjavaid.site/blog/subscription-billing-architecture-laravel-saas" rel="noopener noreferrer"&gt;subscription billing architecture in Laravel&lt;/a&gt; show why this area deserves care.&lt;/p&gt;

&lt;p&gt;🛠 Engineering Service&lt;/p&gt;

&lt;h3&gt;
  
  
  Need specialized help with SaaS Development?
&lt;/h3&gt;

&lt;p&gt;Build scalable SaaS products from MVP to production, including subscriptions, multi-tenant architecture, dashboards, integrations, automation, and complex business workflows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;Discuss Architecture →&lt;/a&gt;&lt;a href="https://aqibjavaid.site/services/saas-development" rel="noopener noreferrer"&gt;About this service&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrations#
&lt;/h3&gt;

&lt;p&gt;Each third-party integration (CRM, accounting, calendar, data providers) adds authentication, error handling and maintenance. Pick the one or two that are essential to the first customers and defer the rest.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-time features#
&lt;/h3&gt;

&lt;p&gt;Live updates, chat and collaborative editing are powerful and costly. Use them only where the product is meaningless without them. On the &lt;a href="https://aqibjavaid.site/work/imperial-votes-nextjs-saas-voting-platform-case-study" rel="noopener noreferrer"&gt;Imperial Votes platform&lt;/a&gt;, a custom online voting product for contests and pageants, I built it from scratch using Next.js, Prisma and PostgreSQL with Stripe and Paystack for payments and Socket.IO for live behaviour. Features like payments and real-time updates were core to that product, so they belonged in the first version. For many other products they would not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom admin and analytics#
&lt;/h3&gt;

&lt;p&gt;Dashboards, reports and exports look small on a wireframe and are large in code. Use off-the-shelf analytics early, and build custom reporting after you know what users ask for.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mobile apps#
&lt;/h3&gt;

&lt;p&gt;A responsive web app often serves an MVP. Native apps double the surface area. Add them when you have evidence that mobile is where users are.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to scope the first version#
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Write the one-sentence promise.&lt;/strong&gt; "Help X do Y without Z." Every feature must serve it.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;List user journeys, not features.&lt;/strong&gt; Sign up, do the core job, pay. If a journey is not needed to learn something, defer it.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Sort into must, should and later.&lt;/strong&gt; Be strict about "must": would the first ten customers refuse to use it without this?&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Replace features with manual processes.&lt;/strong&gt; Onboard customers by hand, invoice the first few manually, review data in a spreadsheet. These are valid MVP techniques.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Buy before you build.&lt;/strong&gt; Use proven services for auth, email, payments and error tracking where it fits.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For a deeper look at decisions to make before development starts, read &lt;a href="https://aqibjavaid.site/blog/technical-decisions-before-building-saas-product" rel="noopener noreferrer"&gt;technical decisions before building a SaaS product&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cost drivers outside the code#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unclear requirements.&lt;/strong&gt; Changes during development cost more than changes in planning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slow feedback.&lt;/strong&gt; Delays in decisions and approvals stretch a project and increase cost.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Team structure.&lt;/strong&gt; A senior engineer working directly with a founder usually moves faster than a layered team, but suits some projects better than others.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Post-launch work.&lt;/strong&gt; Budget for fixes, small improvements and hosting. A common rule of thumb is to keep a reserve for the months after launch rather than spending everything on version one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to compare quotes#
&lt;/h2&gt;

&lt;p&gt;When two quotes differ widely, compare these before you compare price:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Is the scope written down in terms of user journeys?&lt;/li&gt;
&lt;li&gt; Does it include billing, admin, notifications and testing?&lt;/li&gt;
&lt;li&gt; Who owns the code and where is it hosted?&lt;/li&gt;
&lt;li&gt; What is the plan for the first three months after launch?&lt;/li&gt;
&lt;li&gt; Can the work be delivered in phases with a review after each?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A phased approach, with a working slice every couple of weeks, is the best protection against surprises. You see progress, you can change direction and you can stop with something usable.&lt;/p&gt;

&lt;h2&gt;
  
  
  When not to build custom software yet#
&lt;/h2&gt;

&lt;p&gt;If you can test the idea with a no-code tool, a spreadsheet and a payment link, do that first. Custom software is justified when the workflow cannot be faked, or when no-code tools cannot deliver the experience or control you need. I have helped teams move off those tools once they hit limits, as in &lt;a href="https://aqibjavaid.site/blog/migrate-no-code-app-to-nextjs-postgres" rel="noopener noreferrer"&gt;migrating a no-code app to Next.js and Postgres&lt;/a&gt;, but a first prototype in a simpler tool can save months.&lt;/p&gt;

&lt;p&gt;The principle behind all of this is to launch with a small feature set and learn from real users before you invest further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How long does it take to build a SaaS MVP?#
&lt;/h3&gt;

&lt;p&gt;It depends on scope, but a focused first version is typically measured in weeks to a few months, not a year. If the plan says a year, the scope is probably too large.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I use Laravel or Next.js for a SaaS MVP?#
&lt;/h3&gt;

&lt;p&gt;Either can work. Laravel gives you authentication, queues, billing integrations and an admin-friendly structure quickly. Next.js suits products with rich front ends. The choice should follow your product and team, and I compare the options in my &lt;a href="https://aqibjavaid.site/blog/laravel-vs-nodejs-for-saas" rel="noopener noreferrer"&gt;Laravel vs Node.js for SaaS&lt;/a&gt; article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need multi-tenancy from day one?#
&lt;/h3&gt;

&lt;p&gt;If customers are organisations with their own users and data, yes. If your product is for individuals, probably not.&lt;/p&gt;

&lt;h3&gt;
  
  
  What should I keep out of the MVP?#
&lt;/h3&gt;

&lt;p&gt;Anything you cannot connect to a measurable learning goal: advanced reporting, multiple integrations, custom roles, native apps and elaborate settings screens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;MVP cost is driven by scope choices: tenancy, billing, integrations, real-time features and admin tools.&lt;/li&gt;
&lt;li&gt;Check every quote for auth, billing, admin, notifications, infrastructure and testing.&lt;/li&gt;
&lt;li&gt;Define the first version by user journeys and cut anything that does not help you learn.&lt;/li&gt;
&lt;li&gt;Phase the work, review after each phase and keep a reserve for post-launch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are planning a SaaS product and want help turning an idea into a scoped first version, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; or &lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;get in touch&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/saas-mvp-development-cost" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>saas</category>
      <category>mvpdevelopment</category>
      <category>softwareestimation</category>
      <category>startupengineering</category>
    </item>
    <item>
      <title>Laravel N+1 Query Problem: How to Find and Fix It</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Mon, 02 Nov 2026 14:14:03 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/laravel-n1-query-problem-how-to-find-and-fix-it-f83</link>
      <guid>https://dev.to/aqib_javaid/laravel-n1-query-problem-how-to-find-and-fix-it-f83</guid>
      <description>&lt;p&gt;When a Laravel page that used to load in half a second suddenly takes four, the cause is very often the same: the &lt;strong&gt;N+1 query problem&lt;/strong&gt;. One query fetches a list, and then your code quietly runs one more query for every row. With 10 rows nobody notices. With 500 rows the database is doing 501 round trips for a single page.&lt;/p&gt;

&lt;p&gt;This article shows how to find N+1 queries, fix them properly and keep them from returning. It builds on my guide to &lt;a href="https://aqibjavaid.site/blog/laravel-slow-queries-fix" rel="noopener noreferrer"&gt;finding slow queries in Laravel&lt;/a&gt;, and if your whole application feels slow, my &lt;a href="https://aqibjavaid.site/services/database-optimization-backend-performance" rel="noopener noreferrer"&gt;database optimization and backend performance service&lt;/a&gt; is where I start.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the problem looks like#
&lt;/h2&gt;

&lt;p&gt;Imagine a list of orders that shows each customer's name:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$orders = Order::latest()-&amp;gt;take(50)-&amp;gt;get();

foreach ($orders as $order) {
    echo $order-&amp;gt;customer-&amp;gt;name; // runs a query per order
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That is 1 query for orders plus 50 for customers: &lt;strong&gt;51 queries&lt;/strong&gt;. The code looks innocent because relationship access reads like a property. Eloquent loads relations lazily by default, so each first access hits the database.&lt;/p&gt;

&lt;p&gt;It gets worse when it hides in places you do not expect: a Blade loop, an API resource, an accessor that touches a relation, or a policy check inside a loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to find N+1 queries#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use Laravel's built-in guard#
&lt;/h3&gt;

&lt;p&gt;The simplest tool is to make lazy loading an error in non-production environments. In a service provider:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Database\Eloquent\Model;

public function boot(): void
{
    Model::preventLazyLoading(! app()-&amp;gt;isProduction());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now any lazy load throws an exception during development and tests, with the model and relation named. This turns a silent performance bug into a visible failure. The behaviour is documented in the &lt;a href="https://laravel.com/docs/eloquent-relationships#preventing-lazy-loading" rel="noopener noreferrer"&gt;Eloquent relationships docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In production, you can log violations instead of throwing, using &lt;code&gt;Model::handleLazyLoadingViolationUsing()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use a debugging tool#
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Laravel Debugbar&lt;/strong&gt; or &lt;strong&gt;Telescope&lt;/strong&gt; shows the number of queries per request and highlights duplicates.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clockwork&lt;/strong&gt; gives a timeline in the browser.&lt;/li&gt;
&lt;li&gt;For production, an APM or logging setup that records query counts per request lets you find the worst endpoints. I cover this in &lt;a href="https://aqibjavaid.site/blog/laravel-observability-architecture" rel="noopener noreferrer"&gt;observability for Laravel&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Count queries in a test#
&lt;/h3&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB::enableQueryLog();
$this-&amp;gt;getJson('/api/orders')-&amp;gt;assertOk();
$this-&amp;gt;assertLessThan(10, count(DB::getQueryLog()));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Tests that assert a query budget on your busiest endpoints catch regressions before release.&lt;/p&gt;

&lt;p&gt;🛠 Engineering Service&lt;/p&gt;

&lt;h3&gt;
  
  
  Need specialized help with Database Optimization &amp;amp; Backend Performance?
&lt;/h3&gt;

&lt;p&gt;Find and fix database and backend bottlenecks in data-heavy applications, with a focus on query performance, indexing, architecture, and scalability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;Discuss Architecture →&lt;/a&gt;&lt;a href="https://aqibjavaid.site/services/database-optimization-backend-performance" rel="noopener noreferrer"&gt;About this service&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The fixes#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Eager loading with &lt;code&gt;with()&lt;/code&gt;#
&lt;/h3&gt;

&lt;p&gt;Load the relation up front. Laravel runs one extra query for all related records instead of one per row:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$orders = Order::with('customer')-&amp;gt;latest()-&amp;gt;take(50)-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now it is 2 queries, regardless of how many orders there are.&lt;/p&gt;

&lt;p&gt;Eager load nested relations with dot notation, and select only the columns you need:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Order::with(['customer:id,name', 'items.product:id,name,price'])-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Remember to include the key columns Eloquent needs to match records, such as &lt;code&gt;id&lt;/code&gt; and foreign keys.&lt;/p&gt;

&lt;h3&gt;
  
  
  Counts and aggregates with &lt;code&gt;withCount()&lt;/code&gt;#
&lt;/h3&gt;

&lt;p&gt;Showing "12 comments" per post by calling &lt;code&gt;$post-&amp;gt;comments-&amp;gt;count()&lt;/code&gt; loads every comment. Use:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Post::withCount('comments')-&amp;gt;get(); // adds comments_count
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;There are also &lt;code&gt;withSum&lt;/code&gt;, &lt;code&gt;withAvg&lt;/code&gt;, &lt;code&gt;withMin&lt;/code&gt; and &lt;code&gt;withMax&lt;/code&gt; for aggregates computed in SQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional eager loading#
&lt;/h3&gt;

&lt;p&gt;If a relation is only needed sometimes, use &lt;code&gt;load()&lt;/code&gt; or &lt;code&gt;loadMissing()&lt;/code&gt; after the fact:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$orders-&amp;gt;loadMissing('customer');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In API resources, use &lt;code&gt;whenLoaded('customer')&lt;/code&gt; so the resource does not trigger a lazy load by accident.&lt;/p&gt;

&lt;h3&gt;
  
  
  Default eager loads: use sparingly#
&lt;/h3&gt;

&lt;p&gt;You can set &lt;code&gt;$with&lt;/code&gt; on a model to always load certain relations. It is convenient and dangerous, because every query for that model now loads those relations whether they are needed or not. Prefer explicit &lt;code&gt;with()&lt;/code&gt; in the places that need it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where N+1 hides#
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Hiding place&lt;/th&gt;
&lt;th&gt;Why it happens&lt;/th&gt;
&lt;th&gt;Fix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Blade loops&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;@foreach&lt;/code&gt; accesses &lt;code&gt;$item-&amp;gt;relation&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Eager load in the controller&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API resources&lt;/td&gt;
&lt;td&gt;Resource reads a relation per item&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;whenLoaded()&lt;/code&gt; plus eager load&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accessors&lt;/td&gt;
&lt;td&gt;An attribute method touches a relation&lt;/td&gt;
&lt;td&gt;Eager load or compute in SQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Policies and gates&lt;/td&gt;
&lt;td&gt;Authorisation queries per row&lt;/td&gt;
&lt;td&gt;Preload permissions once&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Counts in views&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;-&amp;gt;count()&lt;/code&gt; on a loaded relation&lt;/td&gt;
&lt;td&gt;&lt;code&gt;withCount()&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nested relations&lt;/td&gt;
&lt;td&gt;Parent loaded, children lazily loaded&lt;/td&gt;
&lt;td&gt;Dot notation in &lt;code&gt;with()&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Eager loading is not always the answer#
&lt;/h2&gt;

&lt;p&gt;Sometimes the better fix is different:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pagination:&lt;/strong&gt; do not load thousands of records at all. Paginate or use cursors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Joins or subqueries:&lt;/strong&gt; for sorting or filtering by a related value, a join can be better than loading models.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Chunking:&lt;/strong&gt; for big exports and jobs, use &lt;code&gt;chunkById()&lt;/code&gt; or lazy collections to limit memory, then eager load per chunk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching:&lt;/strong&gt; if the result is expensive and changes rarely, cache it. See &lt;a href="https://aqibjavaid.site/blog/laravel-redis-caching-architecture" rel="noopener noreferrer"&gt;Redis caching in Laravel&lt;/a&gt;, and remember that cache hides a slow query, it does not fix it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Indexes:&lt;/strong&gt; a query that runs once but scans a whole table is a different problem. Check &lt;code&gt;EXPLAIN&lt;/code&gt; and add the right index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eager loading can also overshoot: loading large relations you do not display wastes memory. Select only what you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  A real-world pattern#
&lt;/h2&gt;

&lt;p&gt;Data-heavy dashboards are where N+1 bites hardest. On the &lt;a href="https://aqibjavaid.site/work/violerts-nyc-proptech-compliance-saas" rel="noopener noreferrer"&gt;Violerts NYC compliance platform&lt;/a&gt;, the backend consolidates property data from several city sources and serves it to a React frontend through REST APIs. In any system with related records shown in lists and maps, the habits are the same: eager load what a view needs, aggregate in SQL and keep query counts predictable per endpoint. I treat query count as a metric to watch, in the same way as response time.&lt;/p&gt;

&lt;h2&gt;
  
  
  A simple routine for existing apps#
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; Turn on &lt;code&gt;preventLazyLoading&lt;/code&gt; in local and CI environments.&lt;/li&gt;
&lt;li&gt; Run your main pages and API endpoints and note any exceptions.&lt;/li&gt;
&lt;li&gt; Fix each with &lt;code&gt;with()&lt;/code&gt;, &lt;code&gt;withCount()&lt;/code&gt; or a query change.&lt;/li&gt;
&lt;li&gt; Add query-count assertions to tests for your busiest endpoints.&lt;/li&gt;
&lt;li&gt; In production, log slow requests and high query counts, and review them regularly.&lt;/li&gt;
&lt;li&gt; Re-check after major features. N+1 problems come back as code changes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For broader MySQL tuning (slow log, &lt;code&gt;EXPLAIN&lt;/code&gt;, indexes), the &lt;a href="https://dev.mysql.com/doc/refman/8.0/en/using-explain.html" rel="noopener noreferrer"&gt;MySQL documentation on EXPLAIN&lt;/a&gt; is the reference to keep open.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does Laravel fix N+1 automatically?#
&lt;/h3&gt;

&lt;p&gt;No. By default, Eloquent lazy loads. You need to eager load or enable &lt;code&gt;preventLazyLoading&lt;/code&gt; to catch it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Will eager loading always be faster?#
&lt;/h3&gt;

&lt;p&gt;Usually, for lists. For a single record where you rarely need the relation, lazy loading is fine. Measure rather than assume.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is N+1 still a problem with a fast database?#
&lt;/h3&gt;

&lt;p&gt;Yes. Each query has network and parsing overhead, so hundreds of small queries add up even when each one is fast, and they load the database under traffic.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many queries per page is too many?#
&lt;/h3&gt;

&lt;p&gt;There is no fixed number, but a list page that needs more than a dozen or so queries deserves a look, and counts that grow with the number of rows are a red flag.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;N+1 happens when code lazy loads a relation inside a loop; the query count grows with the row count.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;Model::preventLazyLoading()&lt;/code&gt; in development and tests to make it visible.&lt;/li&gt;
&lt;li&gt;Fix with &lt;code&gt;with()&lt;/code&gt;, &lt;code&gt;withCount()&lt;/code&gt; and &lt;code&gt;loadMissing()&lt;/code&gt;, selecting only needed columns.&lt;/li&gt;
&lt;li&gt;Guard against regressions with query-count tests and production monitoring.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your Laravel application is slow and you want an expert to find out why, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; or &lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;contact me&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/laravel-n-plus-one-query-fix" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>databaseoptimization</category>
      <category>mysql</category>
      <category>performance</category>
    </item>
    <item>
      <title>How to Migrate WordPress to Next.js Without Losing SEO</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Mon, 02 Nov 2026 14:13:33 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/how-to-migrate-wordpress-to-nextjs-without-losing-seo-n1h</link>
      <guid>https://dev.to/aqib_javaid/how-to-migrate-wordpress-to-nextjs-without-losing-seo-n1h</guid>
      <description>&lt;p&gt;Moving a WordPress site to Next.js can make it faster and easier to evolve. Done badly, it can also wipe out years of search rankings in a week. The technology rarely causes that. &lt;strong&gt;Missing redirects, changed URLs, lost metadata and a forgotten sitemap&lt;/strong&gt; do. This guide is the plan I follow to &lt;strong&gt;migrate WordPress to Next.js without losing SEO&lt;/strong&gt;, written for business owners and the developers they hire.&lt;/p&gt;

&lt;p&gt;If you are still deciding whether to move at all, read &lt;a href="https://aqibjavaid.site/blog/nextjs-vs-wordpress-for-business" rel="noopener noreferrer"&gt;Next.js vs WordPress for a business website&lt;/a&gt; first. If you have decided, my &lt;a href="https://aqibjavaid.site/services/react-nextjs-development" rel="noopener noreferrer"&gt;React and Next.js development service&lt;/a&gt; covers builds like this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Principle: keep everything Google already knows#
&lt;/h2&gt;

&lt;p&gt;Search engines have learned your URLs, titles, headings, links and content. A migration is safe when, after launch, the same URLs return the same content (or a clean redirect to its new home), with the same metadata and a fast, crawlable page. Everything below serves that principle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Take a full inventory before touching code#
&lt;/h2&gt;

&lt;p&gt;Build a spreadsheet of what exists now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Every indexed URL: crawl the site and export URLs from your sitemap, analytics and Google Search Console.&lt;/li&gt;
&lt;li&gt;For each URL: title, meta description, H1, canonical, status code, internal links in, organic traffic and impressions.&lt;/li&gt;
&lt;li&gt;Rankings for your key pages and queries.&lt;/li&gt;
&lt;li&gt;Structured data, redirects already in place, and any special files like &lt;code&gt;robots.txt&lt;/code&gt;, &lt;code&gt;ads.txt&lt;/code&gt; or verification files.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Save a copy of this baseline. It is how you will prove, a month later, whether the migration worked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Decide the URL strategy#
&lt;/h2&gt;

&lt;p&gt;The safest approach is to &lt;strong&gt;keep URLs identical&lt;/strong&gt;. WordPress commonly uses paths like &lt;code&gt;/blog/post-name/&lt;/code&gt; with a trailing slash, category archives and pagination. Next.js can reproduce these through its routing and configuration (for example, the &lt;code&gt;trailingSlash&lt;/code&gt; option and dynamic routes).&lt;/p&gt;

&lt;p&gt;If a URL must change, give every old URL a &lt;strong&gt;one-to-one 301 redirect&lt;/strong&gt; to its new equivalent. Avoid redirecting everything to the home page, which Google treats as a soft 404. Avoid chains: old to new in a single hop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Choose how content is managed#
&lt;/h2&gt;

&lt;p&gt;You rarely want to hand-code content into React. Options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Headless WordPress:&lt;/strong&gt; keep WordPress as the editor and fetch content through its API into Next.js. The editing experience stays familiar.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A headless CMS&lt;/strong&gt; such as Strapi or Contentful, with content migrated across.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Markdown or MDX files&lt;/strong&gt; in the repository, which suit developer-run blogs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the &lt;a href="https://aqibjavaid.site/work/nooora-beauty-concierge-nextjs-strapi-seo-case-study" rel="noopener noreferrer"&gt;Nooora Beauty Concierge&lt;/a&gt; site, I joined a project where some sections came from Strapi and others were hard-coded, and client content changes required a developer. The work was moving major content areas into Strapi, adding dynamic service pages and building SEO and sitemap support, so the client could update content themselves on a platform that depends on search visibility. The lesson applies to any migration: &lt;strong&gt;decide who edits content and make the CMS support that person.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Recreate on-page SEO exactly#
&lt;/h2&gt;

&lt;p&gt;For every page, carry over:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Title tags and meta descriptions.&lt;/strong&gt; In the Next.js App Router, use the Metadata API (&lt;code&gt;generateMetadata&lt;/code&gt;) to render them server-side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Canonical URLs&lt;/strong&gt; that point to the preferred version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Heading structure,&lt;/strong&gt; especially H1.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Open Graph and social tags.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Image alt text&lt;/strong&gt; and sensible dimensions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structured data&lt;/strong&gt; (JSON-LD) for articles, products, organisation and breadcrumbs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internal links,&lt;/strong&gt; with the same anchors where possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Next.js docs on &lt;a href="https://nextjs.org/docs/app/building-your-application/optimizing/metadata" rel="noopener noreferrer"&gt;metadata&lt;/a&gt; explain the API. Plugins like Yoast or Rank Math store this data in WordPress, so export it rather than rewriting it by hand.&lt;/p&gt;

&lt;p&gt;🛠 Engineering Service&lt;/p&gt;

&lt;h3&gt;
  
  
  Need specialized help with React &amp;amp; Next.js Development?
&lt;/h3&gt;

&lt;p&gt;Build fast, responsive React and Next.js applications with modern TypeScript architecture, API integrations, reusable components, and production-ready frontend experiences.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;Discuss Architecture →&lt;/a&gt;&lt;a href="https://aqibjavaid.site/services/react-nextjs-development" rel="noopener noreferrer"&gt;About this service&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Render pages so crawlers can read them#
&lt;/h2&gt;

&lt;p&gt;Use server rendering or static generation for public pages, not client-only rendering. Google can process JavaScript, but delivering complete HTML in the first response is faster and more reliable. Next.js does this by default for Server Components. Check the page source after building, not just what you see in the browser.&lt;/p&gt;

&lt;p&gt;Use incremental static regeneration or on-demand revalidation so new posts appear without a full rebuild.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Build redirects, sitemap and robots#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Redirects:&lt;/strong&gt; define them in &lt;code&gt;next.config.js&lt;/code&gt; or middleware. For large lists, load them from a file or a database table, and test every one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sitemap:&lt;/strong&gt; generate &lt;code&gt;sitemap.xml&lt;/code&gt; from your content source and include only canonical, indexable URLs. Submit it in Search Console.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;robots.txt:&lt;/strong&gt; make sure you do not accidentally carry over a staging "disallow all" rule. This is one of the most common launch mistakes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Trailing slash and case rules:&lt;/strong&gt; pick one version and redirect the other.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 7: Check performance and Core Web Vitals#
&lt;/h2&gt;

&lt;p&gt;Speed is one of the reasons to migrate, so confirm it. Use &lt;code&gt;next/image&lt;/code&gt; for responsive images, load fonts efficiently and avoid shipping heavy client-side JavaScript on content pages. Measure with &lt;a href="https://pagespeed.web.dev/" rel="noopener noreferrer"&gt;PageSpeed Insights&lt;/a&gt; before and after. A migration that is slower than the original is a failed migration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Test on staging, thoroughly#
&lt;/h2&gt;

&lt;p&gt;Block staging from search engines with authentication, not just a &lt;code&gt;noindex&lt;/code&gt; tag that might ship to production. Then:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Crawl staging and compare URLs, titles, canonicals and status codes to your baseline.&lt;/li&gt;
&lt;li&gt; Test every redirect from the old URL list.&lt;/li&gt;
&lt;li&gt; Validate structured data.&lt;/li&gt;
&lt;li&gt; Check forms, search, comments and any plugin-powered features you must replace.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I took a similar approach in rebuilding &lt;a href="https://aqibjavaid.site/work/tattoo-panda-wordpress-to-plain-php-contentful" rel="noopener noreferrer"&gt;Tattoo Panda from WordPress to plain PHP with Contentful&lt;/a&gt;, where every page was checked against the original. The method is the same whichever stack you move to: compare the new site to the old one page by page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 9: Launch with care#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Choose a &lt;strong&gt;low-traffic window&lt;/strong&gt; and avoid launching alongside a redesign of the copy, which makes cause and effect impossible to separate.&lt;/li&gt;
&lt;li&gt;Keep the old server available briefly, in case you need to compare or roll back.&lt;/li&gt;
&lt;li&gt;Update DNS, then immediately re-test redirects, robots.txt and the sitemap on the live domain.&lt;/li&gt;
&lt;li&gt;Submit the sitemap and use Search Console's URL Inspection on key pages.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 10: Monitor for 8 to 12 weeks#
&lt;/h2&gt;

&lt;p&gt;Watch:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Search Console:&lt;/strong&gt; Pages report for indexing errors, Performance report for impressions and clicks versus your baseline, and crawl stats.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;404s&lt;/strong&gt; from logs and Search Console, adding redirects for any you missed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rankings&lt;/strong&gt; for priority pages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analytics:&lt;/strong&gt; traffic, conversions and form submissions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A small dip in the first weeks is common as Google re-crawls. A sustained drop after a clean migration usually points to a specific problem, such as missing redirects or changed content, and the baseline helps you find it.&lt;/p&gt;

&lt;p&gt;⚠️Warning&lt;/p&gt;

&lt;p&gt;Do not change domain, URLs, design and content in the same release. Each is a separate risk. Migrate the platform first, change other things afterwards.&lt;/p&gt;

&lt;h2&gt;
  
  
  When not to migrate#
&lt;/h2&gt;

&lt;p&gt;If your WordPress site is fast, easy to edit and ranking well, a migration adds risk with limited benefit. Consider it when you need custom application features, outgrow what plugins can safely do or have performance problems that cannot be fixed within WordPress. For a broader look at the decision to change a system versus improve it, see &lt;a href="https://aqibjavaid.site/blog/rewrite-vs-refactor-legacy-application" rel="noopener noreferrer"&gt;rewrite vs refactor&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Will I lose rankings when I migrate?#
&lt;/h3&gt;

&lt;p&gt;Not if URLs, content and metadata are preserved and redirects are correct. Temporary fluctuation is normal, but a planned migration should return to baseline.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I keep using the WordPress editor?#
&lt;/h3&gt;

&lt;p&gt;Yes, with headless WordPress. You trade some plugin features for a faster front end.&lt;/p&gt;

&lt;h3&gt;
  
  
  How long does a migration take?#
&lt;/h3&gt;

&lt;p&gt;It depends on the number of templates and features, not just the number of pages. A small brochure site can take weeks; a large site with custom features takes longer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need to keep my old URLs?#
&lt;/h3&gt;

&lt;p&gt;It is the safest approach. If you change them, use one-to-one 301 redirects and monitor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Inventory every URL, title and ranking before you start.&lt;/li&gt;
&lt;li&gt;Keep URLs the same, or map each old URL to its new one with a single 301.&lt;/li&gt;
&lt;li&gt;Reproduce metadata, canonicals, structured data and sitemaps, and render HTML on the server.&lt;/li&gt;
&lt;li&gt;Test on a protected staging site, launch carefully and monitor for weeks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are planning a WordPress to Next.js move and want it done without a ranking drop, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; or &lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;contact me&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/migrate-wordpress-to-nextjs-seo" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>wordpress</category>
      <category>seo</category>
      <category>applicationmodernization</category>
    </item>
    <item>
      <title>UPS and FedEx API Integration in Laravel: Real-Time Shipping Rates</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Mon, 02 Nov 2026 14:13:24 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/ups-and-fedex-api-integration-in-laravel-real-time-shipping-rates-5g4p</link>
      <guid>https://dev.to/aqib_javaid/ups-and-fedex-api-integration-in-laravel-real-time-shipping-rates-5g4p</guid>
      <description>&lt;p&gt;If your business ships parcels, showing accurate shipping costs in real time is not optional. Customers abandon carts when shipping is a surprise, and staff waste hours quoting by hand. Both UPS and FedEx publish APIs that return rates for a given package and destination, and wiring them into a Laravel application is very doable. The difficulty is not the first successful request. It is everything that happens when the carrier is slow, the address is wrong or the response format changes.&lt;/p&gt;

&lt;p&gt;This guide covers how I approach &lt;strong&gt;UPS and FedEx API integration in Laravel&lt;/strong&gt;, based on building the rate calculation for a logistics quote system. It is part of the broader work I do in &lt;a href="https://aqibjavaid.site/services/api-development-integrations" rel="noopener noreferrer"&gt;API development and integrations&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the integration needs to do#
&lt;/h2&gt;

&lt;p&gt;Before writing code, be precise about the job. A quote engine has to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Accept package details (weight, dimensions, quantity) and origin and destination addresses.&lt;/li&gt;
&lt;li&gt; Ask each carrier for its available services and prices.&lt;/li&gt;
&lt;li&gt; Convert the different responses into &lt;strong&gt;one common format&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt; Show the user the options, and store the quote so it can be sent, accepted or audited later.&lt;/li&gt;
&lt;li&gt; Keep working, with a clear message, when a carrier is unavailable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;On the &lt;a href="https://aqibjavaid.site/work/expreco-logistics-quote-system" rel="noopener noreferrer"&gt;EXPRECO logistics quote system&lt;/a&gt;, I built this in Laravel and Livewire, integrating UPS and FedEx APIs to calculate real-time shipping rates from package details and destination, plus a ticketing workflow so businesses could respond to customers and share quotes from the platform.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Get credentials and read the docs#
&lt;/h2&gt;

&lt;p&gt;Both carriers require a developer account and application credentials. Their authentication has moved toward OAuth 2.0 client credentials, so check the current documentation rather than relying on older tutorials that describe retired access-key methods. The official portals are the &lt;a href="https://developer.ups.com/" rel="noopener noreferrer"&gt;UPS Developer Portal&lt;/a&gt; and the &lt;a href="https://developer.fedex.com/" rel="noopener noreferrer"&gt;FedEx Developer Portal&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Practical notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start in the &lt;strong&gt;sandbox&lt;/strong&gt; environment. Rates there may not match production, so validate with real accounts before launch.&lt;/li&gt;
&lt;li&gt;Decide whether you need &lt;strong&gt;published rates&lt;/strong&gt; or your &lt;strong&gt;negotiated account rates&lt;/strong&gt;. They are different requests with different credentials.&lt;/li&gt;
&lt;li&gt;Read the terms about caching and displaying rates. Some programmes have rules.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 2: Build a carrier abstraction#
&lt;/h2&gt;

&lt;p&gt;Do not scatter HTTP calls through controllers. Create a small interface that every carrier implements:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface RateProvider
{
    /** @return Collection&amp;lt;int, RateQuote&amp;gt; */
    public function quote(ShipmentRequest $request): Collection;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then write a &lt;code&gt;UpsRateProvider&lt;/code&gt; and a &lt;code&gt;FedExRateProvider&lt;/code&gt; that translate your &lt;code&gt;ShipmentRequest&lt;/code&gt; into each carrier's payload and map the response into a common &lt;code&gt;RateQuote&lt;/code&gt; object (carrier, service name, price, currency, estimated delivery, raw reference ID). A third carrier later becomes one new class instead of a rewrite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Handle authentication tokens properly#
&lt;/h2&gt;

&lt;p&gt;OAuth access tokens are short-lived. Request one, cache it in Laravel's cache until shortly before expiry, and reuse it for every call. Requesting a new token on each rate request wastes time and can trigger rate limits.&lt;/p&gt;

&lt;p&gt;🛠 Engineering Service&lt;/p&gt;

&lt;h3&gt;
  
  
  Need specialized help with API Development &amp;amp; Integrations?
&lt;/h3&gt;

&lt;p&gt;Design and integrate reliable APIs connecting your application with third-party platforms, business systems, payment providers, and external data sources.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;Discuss Architecture →&lt;/a&gt;&lt;a href="https://aqibjavaid.site/services/api-development-integrations" rel="noopener noreferrer"&gt;About this service&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$token = Cache::remember('fedex.token', $ttlSeconds - 60, function () {
    return $this-&amp;gt;requestNewToken();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Wrap the call so that a &lt;code&gt;401&lt;/code&gt; response clears the cached token and retries once. Keep client IDs and secrets in environment variables, never in the repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Use Laravel's HTTP client with timeouts and retries#
&lt;/h2&gt;

&lt;p&gt;The HTTP client makes timeouts and retries a one-liner, and it is where reliability starts:&lt;/p&gt;

&lt;p&gt;php&lt;/p&gt;

&lt;p&gt;Copy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$response = Http::withToken($token)
    -&amp;gt;timeout(8)
    -&amp;gt;retry(2, 200, throw: false)
    -&amp;gt;post($url, $payload);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Choose timeouts that suit the user experience. A checkout page cannot wait 30 seconds. If the carrier does not answer in time, show the other carrier's rates or a fallback, rather than failing the whole page.&lt;/p&gt;

&lt;p&gt;Call both carriers &lt;strong&gt;in parallel&lt;/strong&gt; using &lt;code&gt;Http::pool()&lt;/code&gt; so the user waits for the slowest one, not the sum of both.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Normalise and validate the response#
&lt;/h2&gt;

&lt;p&gt;Carriers return different structures, units and service codes. Map them carefully:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Convert currencies and rounding in one place.&lt;/li&gt;
&lt;li&gt;Translate service codes into names your users understand.&lt;/li&gt;
&lt;li&gt;Reject responses with missing prices instead of showing zero.&lt;/li&gt;
&lt;li&gt;Log the raw request and response identifiers so you can debug disputes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Validate input before you call anyone. Missing postcodes, impossible weights and bad dimensions are the most common causes of carrier errors. Show the user a plain message and let them correct it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Cache sensibly#
&lt;/h2&gt;

&lt;p&gt;Identical requests (same package, same route) within a short window can reuse results. A cache key built from the normalised request and a short TTL cuts calls and speeds up the UI. Keep the TTL short, since rates can change with fuel surcharges and service levels, and check each carrier's terms. My notes on &lt;a href="https://aqibjavaid.site/blog/laravel-redis-caching-architecture" rel="noopener noreferrer"&gt;Redis caching architecture in Laravel&lt;/a&gt; cover key design and invalidation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 7: Plan for failure#
&lt;/h2&gt;

&lt;p&gt;Carrier APIs go down and return odd errors. Decide up front what the user sees:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One carrier fails:&lt;/strong&gt; show the other's rates, with a note if relevant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Both fail:&lt;/strong&gt; allow the quote to be saved as "pending" and processed by a queued job, or let staff quote manually.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Address problems:&lt;/strong&gt; surface the carrier's message in plain language.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also respect carrier rate limits. If many users hit your quote form at once, you may need to queue requests or throttle them. My article on &lt;a href="https://aqibjavaid.site/blog/laravel-api-rate-limiting-architecture" rel="noopener noreferrer"&gt;API rate limiting architecture&lt;/a&gt; covers the incoming side of the problem, and the same ideas of back-off and limits apply when you are the client.&lt;/p&gt;

&lt;p&gt;ℹ️Note&lt;/p&gt;

&lt;p&gt;Always store the quote you showed the customer. If the price changes by the time they accept it, you need the original to resolve the dispute.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 8: Test without hitting the carrier#
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;Http::fake()&lt;/code&gt; to return recorded sample responses in your test suite so tests are fast and deterministic. Keep a handful of real recorded payloads for edge cases: oversized packages, remote areas, multi-package shipments. Add a few sandbox tests you run manually before each release.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shipping rates vs full shipping#
&lt;/h2&gt;

&lt;p&gt;Rates are the easiest part. Creating labels, tracking shipments, handling customs documents for international orders and managing returns all add separate integrations. Decide on scope early so a "show me rates" project does not quietly become a logistics platform. For an overview of how integration projects are scoped and what influences their cost, read &lt;a href="https://aqibjavaid.site/blog/api-integration-cost-timeline" rel="noopener noreferrer"&gt;API integration cost and timeline&lt;/a&gt;, and for general design rules see &lt;a href="https://aqibjavaid.site/blog/third-party-api-integration-best-practices" rel="noopener noreferrer"&gt;third-party API integration best practices&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Should I use a shipping aggregator instead of direct carrier APIs?#
&lt;/h3&gt;

&lt;p&gt;An aggregator gives you many carriers through one integration and may include labels and tracking. The trade-off is fees and less control. Direct APIs make sense when you use one or two carriers and want negotiated rates and full control.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need Livewire or a frontend framework?#
&lt;/h3&gt;

&lt;p&gt;No. The integration is backend work. Livewire is convenient for live quote forms, but a standard form or a JavaScript client works equally well.&lt;/p&gt;

&lt;h3&gt;
  
  
  How accurate are the rates?#
&lt;/h3&gt;

&lt;p&gt;Accuracy depends on the data you send and the account credentials used. Wrong dimensions or addresses produce wrong prices, and surcharges may appear later. Validate input and store the original quote.&lt;/p&gt;

&lt;h3&gt;
  
  
  How long does an integration like this take?#
&lt;/h3&gt;

&lt;p&gt;A single carrier's rate lookup is usually days of work. Two carriers with a clean abstraction, error handling and tests is more. Labels and tracking add more again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Wrap each carrier behind a common interface and return one normalised quote format.&lt;/li&gt;
&lt;li&gt;Cache OAuth tokens, set timeouts, retry sensibly and call carriers in parallel.&lt;/li&gt;
&lt;li&gt;Decide what happens when a carrier fails before you launch.&lt;/li&gt;
&lt;li&gt;Store every quote shown to a customer, and test with faked responses.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you need rates or full shipping integrated into your application, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; or &lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;get in touch&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/ups-fedex-api-integration-laravel" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>apiintegration</category>
      <category>restapis</category>
      <category>ecommercedevelopment</category>
    </item>
    <item>
      <title>RAG vs Fine-Tuning for a Business Chatbot: Which to Choose</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Mon, 02 Nov 2026 14:12:55 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/rag-vs-fine-tuning-for-a-business-chatbot-which-to-choose-fd8</link>
      <guid>https://dev.to/aqib_javaid/rag-vs-fine-tuning-for-a-business-chatbot-which-to-choose-fd8</guid>
      <description>&lt;p&gt;When a business decides it wants an AI chatbot that "knows our company," the next question is usually technical: should we &lt;strong&gt;fine-tune a model on our data, or use retrieval-augmented generation (RAG)?&lt;/strong&gt; The two are often described as competitors. They solve different problems, and for most business chatbots the answer is clear once you ask the right questions. This is the framework I use when advising on &lt;a href="https://aqibjavaid.site/services/ai-integration-development" rel="noopener noreferrer"&gt;AI integration projects&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The short answer: &lt;strong&gt;use RAG for knowledge, use fine-tuning for behaviour.&lt;/strong&gt; If your goal is for the bot to answer questions about your products, policies or documents correctly, start with RAG.&lt;/p&gt;

&lt;h2&gt;
  
  
  What each approach actually does#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  RAG: look it up, then answer#
&lt;/h3&gt;

&lt;p&gt;In retrieval-augmented generation, your documents are split into chunks and turned into embeddings that are stored in a vector index. When a user asks a question, the system finds the most relevant chunks, adds them to the prompt and asks the model to answer using only that context. The model's weights are not changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fine-tuning: change the model#
&lt;/h3&gt;

&lt;p&gt;Fine-tuning continues training a model on examples you provide. It changes how the model writes or behaves: tone, format, handling of a specific task. It is not a reliable way to teach it a large body of changing facts, and a fine-tuned model can still produce confident wrong answers about those facts.&lt;/p&gt;

&lt;h2&gt;
  
  
  The questions that decide it#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How often does your knowledge change?#
&lt;/h3&gt;

&lt;p&gt;Prices, policies, documentation and product details change. With RAG, you update a document and the bot knows within minutes of re-indexing. With fine-tuning, you must prepare new training data and run training again. If your information changes monthly or faster, RAG is the practical choice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do answers need to be traceable?#
&lt;/h3&gt;

&lt;p&gt;For support, compliance or anything where "where did that come from?" matters, RAG lets you show the source passages and link to them. A fine-tuned model cannot cite what it learned, because that knowledge is spread across its weights.&lt;/p&gt;

&lt;h3&gt;
  
  
  Who needs to control what the bot says?#
&lt;/h3&gt;

&lt;p&gt;With RAG, you can restrict the bot to a defined set of documents, add topic rules and handle "I don't know" cleanly. On the &lt;a href="https://aqibjavaid.site/work/henceforward-ai-rag-chatbot-knowledge-base" rel="noopener noreferrer"&gt;Henceforward AI chatbot and knowledge base&lt;/a&gt;, I built an admin suite for brand persona, topic restrictions and AI guardrails around a RAG pipeline that ingests web pages, documents and visual assets. Business owners needed control without involving a developer each time the content changed, which is a RAG strength.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are you trying to change: facts or style?#
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Goal&lt;/th&gt;
&lt;th&gt;Better fit&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Answer questions from company documents&lt;/td&gt;
&lt;td&gt;RAG&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Keep answers current as content changes&lt;/td&gt;
&lt;td&gt;RAG&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cite sources to the user&lt;/td&gt;
&lt;td&gt;RAG&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Match a specific writing tone or output format&lt;/td&gt;
&lt;td&gt;Fine-tuning (or careful prompting)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Perform a narrow classification or extraction task at scale&lt;/td&gt;
&lt;td&gt;Fine-tuning&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reduce prompt size for a repeated task&lt;/td&gt;
&lt;td&gt;Fine-tuning&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  What can you afford to maintain?#
&lt;/h3&gt;

&lt;p&gt;RAG has ongoing work: ingestion pipelines, chunking, an index, evaluation. Fine-tuning has its own: dataset curation, training runs, evaluation and re-training when the base model changes. Neither is free, but RAG's costs are usually easier to predict for knowledge-heavy bots. For budgeting guidance, see &lt;a href="https://aqibjavaid.site/blog/ai-chatbot-development-cost" rel="noopener noreferrer"&gt;AI chatbot development cost&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;🛠 Engineering Service&lt;/p&gt;

&lt;h3&gt;
  
  
  Need specialized help with AI Integration &amp;amp; AI-Powered Applications?
&lt;/h3&gt;

&lt;p&gt;Integrate AI into existing products or build AI-powered applications that automate workflows, analyze data, generate content, and improve business operations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;Discuss Architecture →&lt;/a&gt;&lt;a href="https://aqibjavaid.site/services/ai-integration-development" rel="noopener noreferrer"&gt;About this service&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why RAG is the default for business knowledge#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Freshness.&lt;/strong&gt; Update the source, not the model.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control.&lt;/strong&gt; You decide what the model can see.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Auditability.&lt;/strong&gt; You can log which passages produced each answer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lower risk of hallucination&lt;/strong&gt; when the prompt requires answers to come from the supplied context. It reduces, but does not eliminate, wrong answers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Model portability.&lt;/strong&gt; Your knowledge base is yours. You can swap the language model later without retraining.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Where RAG goes wrong#
&lt;/h2&gt;

&lt;p&gt;RAG is not magic. Most "the bot gave a wrong answer" reports I debug trace back to retrieval, not the model: poor chunking, missing documents, ambiguous queries, or no instruction on what to do when nothing relevant is found. I cover the diagnosis steps in &lt;a href="https://aqibjavaid.site/blog/rag-chatbot-wrong-answers-fix" rel="noopener noreferrer"&gt;fixing RAG chatbot wrong answers&lt;/a&gt;. The index choice also matters at larger scale, which I compare in &lt;a href="https://aqibjavaid.site/blog/pgvector-hnsw-vs-ivfflat-index" rel="noopener noreferrer"&gt;pgvector HNSW vs IVFFlat&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;💡Tip&lt;/p&gt;

&lt;p&gt;Before you tune anything, build a small set of real questions with known good answers. It is the cheapest way to tell whether a change helped or hurt.&lt;/p&gt;

&lt;h2&gt;
  
  
  When fine-tuning makes sense#
&lt;/h2&gt;

&lt;p&gt;Fine-tuning earns its place when you have a &lt;strong&gt;narrow, repeatable task&lt;/strong&gt; and a clean set of examples, such as extracting fields in a fixed format, classifying tickets into your categories, or writing in a very specific voice at volume. Even then, try careful prompting and a few examples in the prompt first. Fine-tuning adds a training pipeline you have to own.&lt;/p&gt;

&lt;h2&gt;
  
  
  The combined approach#
&lt;/h2&gt;

&lt;p&gt;Many mature systems use both: a fine-tuned or well-prompted model for tone and structure, with RAG supplying the facts. If you are starting out, build RAG first. It gets you to useful faster and tells you where fine-tuning might add value.&lt;/p&gt;

&lt;p&gt;For general background on the technique, the original paper is &lt;a href="https://arxiv.org/abs/2005.11401" rel="noopener noreferrer"&gt;Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks&lt;/a&gt;, and Anthropic's &lt;a href="https://docs.anthropic.com/en/docs/build-with-claude/embeddings" rel="noopener noreferrer"&gt;documentation on embeddings&lt;/a&gt; explains how retrieval fits into an application.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical way to start#
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; Collect the 20 to 50 questions your users actually ask.&lt;/li&gt;
&lt;li&gt; Gather the documents that contain the answers, and note gaps.&lt;/li&gt;
&lt;li&gt; Build a basic RAG pipeline with source citations.&lt;/li&gt;
&lt;li&gt; Test it against your question list and record failures.&lt;/li&gt;
&lt;li&gt; Add guardrails: scope limits, refusal behaviour and handoff to a human.&lt;/li&gt;
&lt;li&gt; Only consider fine-tuning if the remaining failures are about behaviour, not knowledge.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is how production features are built. I talk about the habits that separate demos from reliable systems in &lt;a href="https://aqibjavaid.site/blog/production-grade-ai-features" rel="noopener noreferrer"&gt;production-grade AI features&lt;/a&gt;. On &lt;a href="https://aqibjavaid.site/work/safetyspace" rel="noopener noreferrer"&gt;SafetySpace&lt;/a&gt;, the AI sits inside configurable safety workflows where reliable access to information matters, which is a similar kind of problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Frequently asked questions#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can a fine-tuned model replace a knowledge base?#
&lt;/h3&gt;

&lt;p&gt;Not reliably. It may reproduce some facts but cannot guarantee accuracy or point to a source, and it goes stale as your information changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is RAG more expensive than fine-tuning?#
&lt;/h3&gt;

&lt;p&gt;It depends on volume. RAG adds retrieval and larger prompts per request. Fine-tuning adds training and evaluation costs up front. For most business chatbots, RAG is cheaper to launch and easier to keep current.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does RAG stop hallucinations?#
&lt;/h3&gt;

&lt;p&gt;It reduces them by grounding answers in your documents, but it does not remove them. You still need clear "I don't know" behaviour and testing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need a vector database?#
&lt;/h3&gt;

&lt;p&gt;Not necessarily. For many projects, Postgres with the pgvector extension is enough, and it keeps your stack simple.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;RAG is for knowledge; fine-tuning is for behaviour.&lt;/li&gt;
&lt;li&gt;Choose RAG when information changes, answers must be sourced or content owners need control.&lt;/li&gt;
&lt;li&gt;Most wrong answers come from retrieval, so test against real questions.&lt;/li&gt;
&lt;li&gt;Start with RAG, and add fine-tuning only for a clearly defined behavioural gap.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are planning a chatbot and want to choose the right architecture, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; or &lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;contact me&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/rag-vs-fine-tuning-business-chatbot" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>rag</category>
      <category>llm</category>
      <category>aichatbot</category>
      <category>vectorsearch</category>
    </item>
    <item>
      <title>GoHighLevel Stripe Integration: Charge Leads Automatically</title>
      <dc:creator>Aqib Javaid</dc:creator>
      <pubDate>Mon, 02 Nov 2026 14:12:46 +0000</pubDate>
      <link>https://dev.to/aqib_javaid/gohighlevel-stripe-integration-charge-leads-automatically-ddh</link>
      <guid>https://dev.to/aqib_javaid/gohighlevel-stripe-integration-charge-leads-automatically-ddh</guid>
      <description>&lt;p&gt;GoHighLevel is excellent for managing leads, pipelines and follow-up. Billing is where agencies hit its limits. If you sell leads and want to charge each client when a qualified lead is delivered, a flat subscription does not fit, and charging by hand does not scale. This guide explains how a &lt;strong&gt;GoHighLevel Stripe integration&lt;/strong&gt; for pay-per-lead billing works, based on a workflow I built for a lead-generation business. Custom work like this sits within my &lt;a href="https://aqibjavaid.site/services/api-development-integrations" rel="noopener noreferrer"&gt;API development and integrations service&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The core idea: let the client &lt;strong&gt;save a payment method once without paying anything&lt;/strong&gt;, then &lt;strong&gt;charge it automatically&lt;/strong&gt; whenever an event in GoHighLevel says a lead has been delivered. Everything between those two moments needs to be safe, idempotent and visible to both systems.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with the built-in options#
&lt;/h2&gt;

&lt;p&gt;Native payment links and invoices assume the amount is known up front. Pay-per-lead billing breaks that assumption in three ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The amount depends on &lt;strong&gt;how many qualified leads&lt;/strong&gt; are delivered.&lt;/li&gt;
&lt;li&gt;Charges happen &lt;strong&gt;later&lt;/strong&gt;, when the client is not on the page.&lt;/li&gt;
&lt;li&gt;Failures matter: a declined card means a lead was delivered and not paid for.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So you need a flow that separates "authorise the card" from "take the money", and a link between payment results and the CRM.&lt;/p&gt;

&lt;h2&gt;
  
  
  The architecture in one picture#
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; The client receives a secure link and enters card details on a page that uses Stripe.js, so card numbers never touch your server.&lt;/li&gt;
&lt;li&gt; A &lt;strong&gt;SetupIntent&lt;/strong&gt; saves the card against a Stripe customer, with no charge.&lt;/li&gt;
&lt;li&gt; When a lead is marked as delivered in GoHighLevel, a trigger calls your integration.&lt;/li&gt;
&lt;li&gt; Your service creates an &lt;strong&gt;off-session PaymentIntent&lt;/strong&gt; for the saved card.&lt;/li&gt;
&lt;li&gt; Stripe sends &lt;strong&gt;webhooks&lt;/strong&gt; for success or failure, and your service updates GoHighLevel so the pipeline reflects reality.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is the pattern behind the &lt;a href="https://aqibjavaid.site/work/stripe-lead-billing-gohighlevel" rel="noopener noreferrer"&gt;Stripe lead billing workflow I built for a GoHighLevel client&lt;/a&gt;: a secure flow that saves payment methods without an immediate charge, charges them automatically when qualified leads are delivered, and uses webhook-driven status synchronisation for successful and failed payments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Save the card with a SetupIntent#
&lt;/h2&gt;

&lt;p&gt;A SetupIntent is Stripe's way to collect a payment method for future use. You create a Stripe customer, create a SetupIntent for that customer, and confirm it on the client side with Stripe.js. Once confirmed, the payment method is attached to the customer.&lt;/p&gt;

&lt;p&gt;Things that matter here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ask for the right &lt;code&gt;usage&lt;/code&gt; (&lt;code&gt;off_session&lt;/code&gt;) so the card can be charged when the client is absent.&lt;/li&gt;
&lt;li&gt;Handle &lt;strong&gt;authentication&lt;/strong&gt; (3D Secure) at setup time, so later charges are less likely to be interrupted.&lt;/li&gt;
&lt;li&gt;Store the Stripe customer ID and payment method ID against the CRM contact, as custom fields or in your own database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stripe documents this flow in its &lt;a href="https://docs.stripe.com/payments/save-and-reuse" rel="noopener noreferrer"&gt;guide to saving payment details&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Trigger charges from GoHighLevel#
&lt;/h2&gt;

&lt;p&gt;GoHighLevel can call an external URL when something happens, for example when an opportunity moves to a "Lead delivered" stage. Use that trigger to send a request to your own endpoint with the contact ID and the amount or lead count. Options include a webhook action in a workflow or an automation tool such as Zapier if you prefer to keep logic outside custom code.&lt;/p&gt;

&lt;p&gt;🛠 Engineering Service&lt;/p&gt;

&lt;h3&gt;
  
  
  Need specialized help with API Development &amp;amp; Integrations?
&lt;/h3&gt;

&lt;p&gt;Design and integrate reliable APIs connecting your application with third-party platforms, business systems, payment providers, and external data sources.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;Discuss Architecture →&lt;/a&gt;&lt;a href="https://aqibjavaid.site/services/api-development-integrations" rel="noopener noreferrer"&gt;About this service&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do not let the CRM decide the amount blindly. Your endpoint should:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Verify the request really came from your workflow (a shared secret or signature).&lt;/li&gt;
&lt;li&gt; Look up the contact's saved Stripe customer and payment method.&lt;/li&gt;
&lt;li&gt; Calculate the amount on the server from trusted data.&lt;/li&gt;
&lt;li&gt; Create the PaymentIntent with &lt;code&gt;off_session: true&lt;/code&gt; and &lt;code&gt;confirm: true&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 3: Make charges idempotent#
&lt;/h2&gt;

&lt;p&gt;Automations retry. Workflows fire twice. Networks time out. If your endpoint can be called twice for the same lead, you must never charge twice.&lt;/p&gt;

&lt;p&gt;Use an &lt;strong&gt;idempotency key&lt;/strong&gt; derived from the lead or opportunity ID when creating the PaymentIntent. Stripe will return the original result for repeated requests with the same key. I wrote about the same idea for APIs in general in &lt;a href="https://aqibjavaid.site/blog/laravel-api-idempotency-keys" rel="noopener noreferrer"&gt;idempotency keys in Laravel APIs&lt;/a&gt;, and the principle applies to any stack.&lt;/p&gt;

&lt;p&gt;📌Important&lt;/p&gt;

&lt;p&gt;Record each charge attempt in your own database before calling Stripe. If the call or response is lost, you still have a record to reconcile.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Use webhooks as the source of truth#
&lt;/h2&gt;

&lt;p&gt;A synchronous response from Stripe tells you what happened right now. Webhooks tell you what is true. Some payments finish later, some need further action and some fail after initially looking fine.&lt;/p&gt;

&lt;p&gt;Subscribe to events such as &lt;code&gt;payment_intent.succeeded&lt;/code&gt; and &lt;code&gt;payment_intent.payment_failed&lt;/code&gt;, then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Verify the signature&lt;/strong&gt; on every webhook using Stripe's signing secret.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;De-duplicate events&lt;/strong&gt; by event ID, because Stripe may deliver the same one more than once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Update GoHighLevel&lt;/strong&gt; through its API: move the opportunity, set a "Paid" or "Payment failed" tag, or write a custom field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I have written a full walkthrough of safe webhook handling in &lt;a href="https://aqibjavaid.site/blog/stripe-webhooks-laravel-verify-dedupe-retry" rel="noopener noreferrer"&gt;Stripe webhooks: verify, dedupe, retry&lt;/a&gt;. The principles are not specific to Laravel, and Stripe's own &lt;a href="https://docs.stripe.com/webhooks" rel="noopener noreferrer"&gt;webhook documentation&lt;/a&gt; covers signature verification in several languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Plan for failed payments#
&lt;/h2&gt;

&lt;p&gt;Failures are not an edge case in pay-per-lead billing. Decide up front:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What happens when a card is declined?&lt;/strong&gt; Notify the client, pause new lead delivery or both.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How is a retry handled?&lt;/strong&gt; Offer a link to update the card, then re-attempt the failed charge.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Who sees it?&lt;/strong&gt; Your team should get an alert, and the CRM record should show the state, so nobody has to open Stripe to find out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What about disputes and refunds?&lt;/strong&gt; Make sure refunds update the CRM status too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Make the payment state visible inside GoHighLevel. A pipeline that shows "Delivered, paid" or "Delivered, payment failed" is what makes the integration useful for the people who work in it daily.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it costs to build#
&lt;/h2&gt;

&lt;p&gt;A basic version, with one Stripe account, one card-saving page, one trigger and two webhook events, is a small project. It grows with more pricing rules (tiers, discounts, credits), multi-account or multi-currency needs, invoicing and reporting. For general guidance on estimating this kind of work, see &lt;a href="https://aqibjavaid.site/blog/api-integration-cost-timeline" rel="noopener noreferrer"&gt;API integration cost and timeline&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When not to build this#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;If you charge a fixed monthly fee, &lt;strong&gt;Stripe Billing subscriptions&lt;/strong&gt; are simpler and need no custom charge logic.&lt;/li&gt;
&lt;li&gt;If volume is a handful of clients, &lt;strong&gt;manual invoicing&lt;/strong&gt; may be fine until it hurts.&lt;/li&gt;
&lt;li&gt;If you cannot hold customer card data under your compliance comfort level, keep everything with Stripe-hosted components and never store card details yourself.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frequently asked questions#
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Does GoHighLevel connect to Stripe natively?#
&lt;/h3&gt;

&lt;p&gt;It has a native Stripe connection for its own payment features, such as invoices and products. Charging on a custom trigger like "lead delivered" usually needs extra logic through workflows or custom code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is it safe to charge a saved card without the customer present?#
&lt;/h3&gt;

&lt;p&gt;Yes, if the customer authorised future charges when saving the card and you follow Stripe's off-session flow. Be clear in your terms about what will be charged and when.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if a charge needs authentication?#
&lt;/h3&gt;

&lt;p&gt;Off-session payments can fail if the bank demands authentication. Handle the resulting error by sending the customer a link to complete it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can I do this with Zapier instead of code?#
&lt;/h3&gt;

&lt;p&gt;For simple flows, yes. For anything involving idempotency, signature checks and reconciliation, custom code is more reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key takeaways#
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Separate saving the card (SetupIntent) from charging it (off-session PaymentIntent).&lt;/li&gt;
&lt;li&gt;Make the charge endpoint idempotent and calculate amounts on the server.&lt;/li&gt;
&lt;li&gt;Treat Stripe webhooks as the source of truth and sync results back to GoHighLevel.&lt;/li&gt;
&lt;li&gt;Design the failed-payment path before launch, not after the first decline.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you need a billing workflow like this connected to your CRM, &lt;a href="https://aqibjavaid.site/book" rel="noopener noreferrer"&gt;book a call&lt;/a&gt; or &lt;a href="https://aqibjavaid.site/contact" rel="noopener noreferrer"&gt;contact me&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Originally published at &lt;a href="https://aqibjavaid.site/blog/gohighlevel-stripe-integration" rel="noopener noreferrer"&gt;aqibjavaid.site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>stripe</category>
      <category>webhooks</category>
      <category>apiintegration</category>
      <category>payments</category>
    </item>
  </channel>
</rss>
