<?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: Abdulrehman Khan</title>
    <description>The latest articles on DEV Community by Abdulrehman Khan (@arkhan0072).</description>
    <link>https://dev.to/arkhan0072</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%2F1203074%2F62278bf3-5653-48a3-9e52-9a8f16919e60.jpg</url>
      <title>DEV Community: Abdulrehman Khan</title>
      <link>https://dev.to/arkhan0072</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arkhan0072"/>
    <language>en</language>
    <item>
      <title>ZATCA e-invoicing in Laravel: the invoice chain is the part that breaks</title>
      <dc:creator>Abdulrehman Khan</dc:creator>
      <pubDate>Fri, 27 Nov 2026 06:40:22 +0000</pubDate>
      <link>https://dev.to/arkhan0072/zatca-e-invoicing-in-laravel-the-invoice-chain-is-the-part-that-breaks-4gad</link>
      <guid>https://dev.to/arkhan0072/zatca-e-invoicing-in-laravel-the-invoice-chain-is-the-part-that-breaks-4gad</guid>
      <description>&lt;p&gt;By 1 February 2027, ZATCA's 25th integration wave must connect to Phase 2 e-invoicing: every Saudi taxpayer whose VAT-able revenue passed SAR 187,500 in any year from 2022 to 2025, which covers almost every VAT-registered business. Many Laravel apps that sell in Saudi Arabia now have to sign every invoice, report it to ZATCA within 24 hours, and have business-to-business invoices cleared by ZATCA before they go out.&lt;/p&gt;

&lt;p&gt;I built &lt;a href="https://github.com/arkhan0072-dev/laravel-zatca" rel="noopener noreferrer"&gt;laravel-zatca&lt;/a&gt; for that. This post explains the problem it focuses on, how to use it, and when another package is the better choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  The part that breaks: the invoice chain
&lt;/h2&gt;

&lt;p&gt;Every invoice an EGS unit (an invoicing device or system) issues carries two values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the invoice counter value (ICV), which must go up by one for each invoice;&lt;/li&gt;
&lt;li&gt;the previous invoice hash (PIH), the hash of the unit's previous invoice.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ZATCA rejects an invoice whose counter or previous hash is wrong, and every later invoice of that unit fails with it. In a demo this never happens. In production, two web requests or two queue workers issue invoices at the same moment, both read the same "last invoice", and both build the same counter and the same previous hash.&lt;/p&gt;

&lt;p&gt;laravel-zatca locks the unit's chain head (&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;) while an invoice is built, signed and stored. A second invoice waits for the first and then gets the next link. If building or signing fails, nothing is stored and no counter is used up.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require arkhan0072-dev/laravel-zatca
php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;PHP 8.3 and 8.4, Laravel 12 and 13, MySQL, PostgreSQL or SQLite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Onboard a unit
&lt;/h2&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Zatca&lt;/span&gt;\&lt;span class="nc"&gt;Onboarding&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Zatca&lt;/span&gt;\&lt;span class="nc"&gt;UnitDetails&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$onboarding&lt;/span&gt; &lt;span class="o"&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;Onboarding&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="nv"&gt;$onboarding&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pos-1'&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;UnitDetails&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;vatNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'399999999900003'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;organizationName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Maximum Speed Tech Supply'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;branchName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Riyadh Branch'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;commonName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'TST-886431145-399999999900003'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;solutionName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'TST'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'TST'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;serialNumber&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'ed22f1d8-e6a2-1118-9b58-d9a8f11e445f'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;address&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'RRRD2929'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;businessCategory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'Supply activities'&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;$otp&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 OTP from the taxpayer's Fatoora portal&lt;/span&gt;

&lt;span class="nv"&gt;$onboarding&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pos-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;$standardSample&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$simplifiedSample&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;start()&lt;/code&gt; generates the key pair and certificate request and gets the compliance certificate. &lt;code&gt;complete()&lt;/code&gt; passes ZATCA's compliance checks with your sample documents and stores the production certificate. The private key and secrets are encrypted with your &lt;code&gt;APP_KEY&lt;/code&gt;, and the key is never written to disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Issue and send an invoice
&lt;/h2&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Zatca&lt;/span&gt;\&lt;span class="nc"&gt;Jobs&lt;/span&gt;\&lt;span class="nc"&gt;SubmitInvoice&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Zatca&lt;/span&gt;\&lt;span class="nc"&gt;Zatca&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$invoice&lt;/span&gt; &lt;span class="o"&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;Zatca&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;issue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pos-1'&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="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// signed UBL 2.1 XML, hash and QR code&lt;/span&gt;

&lt;span class="nf"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SubmitInvoice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$invoice&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;Simplified (B2C) invoices go to reporting and standard (B2B) invoices to clearance. Each invoice is sent exactly once: the row is locked while it is sent, a reported or cleared invoice is never sent again, and ZATCA's "already reported" answer counts as success. A rejection is stored with ZATCA's reasons and not retried. An outage leaves the invoice pending, and the job retries it with backoff. For cleared invoices, ZATCA's stamped XML is kept, since that is the copy the buyer should receive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Refunds, the 24-hour window and expiring certificates
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$note&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$zatca&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;creditNote&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$invoice&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$noteData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Customer returned one item'&lt;/span&gt;&lt;span class="p"&gt;)&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;'zatca:submit-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;everyFifteenMinutes&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;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Onboarding&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;renew&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pos-1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$otp&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;Credit and debit notes go into the same chain as their invoice and are reported or cleared the same way. The scheduled command sends anything still pending, warns about simplified invoices close to ZATCA's 24-hour limit and certificates that expire within 30 days, and fails when anything was rejected, so your monitoring notices. &lt;code&gt;InvoiceReported&lt;/code&gt;, &lt;code&gt;InvoiceCleared&lt;/code&gt; and &lt;code&gt;InvoiceRejected&lt;/code&gt; events let you email the buyer the cleared copy or alert on a rejection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test your app without ZATCA
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$fake&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Zatca&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="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// ... your code onboards, issues and submits ...&lt;/span&gt;

&lt;span class="nv"&gt;$fake&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;assertReported&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$invoice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$fake&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;rejectNext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'BR-KSA-37'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'The seller address building number must contain 4 digits.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$fake&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;outage&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;// ZATCA answers 503 until $fake-&amp;gt;outage(false)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When to use it, and when not
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;What it covers&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/SallaApp/ZATCA" rel="noopener noreferrer"&gt;salla/zatca&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;Signing an XML invoice you build and generating its QR code. The most used PHP package. No onboarding, no submission, no chain.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/Saleh7/php-zatca-xml" rel="noopener noreferrer"&gt;saleh7/php-zatca-xml&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;XML generation, signing, certificates and the ZATCA API client, in plain PHP. laravel-zatca uses it for exactly these parts.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;laravel-zatca&lt;/td&gt;
&lt;td&gt;The Laravel layer around them: a concurrency-safe chain, stored and encrypted credentials, exactly-once queued submission, and test fakes.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you only need a QR code for Phase 1, salla/zatca is enough. If you are not on Laravel, use saleh7/php-zatca-xml directly.&lt;/p&gt;

&lt;p&gt;laravel-zatca is at 1.0, which means a stable API: breaking changes wait for 2.0. Its whole flow passes ZATCA's developer sandbox, and the chain holds under four concurrent processes on PostgreSQL and MySQL (that test found and fixed a MySQL deadlock before 1.0). It has not yet been through a real taxpayer's onboarding in ZATCA's simulation environment, so do that before going live. If your deadline is close and you need a supported product, look at a commercial e-invoicing provider as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Books, not just invoices
&lt;/h2&gt;

&lt;p&gt;ZATCA needs the legal invoice; your accounts need the money. &lt;a href="https://github.com/arkhan0072-dev/laravel-ledger" rel="noopener noreferrer"&gt;laravel-ledger&lt;/a&gt;, from the same author, books each issued invoice once, linked to it, so the VAT you owe and your profit and loss follow from your invoices. Each package works on its own:&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;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ZATCA invoice {$data['id']}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23_115&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sales&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20_100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$vatPayable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3_015&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="n"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"zatca:{$invoice-&amp;gt;uuid}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$invoice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How it is tested
&lt;/h2&gt;

&lt;p&gt;Every feature was written test first. CI runs the suite on PHP 8.3 and 8.4, Laravel 12 and 13, and MySQL, PostgreSQL and SQLite, with and without laravel-ledger installed, and fails below 100% line coverage. An opt-in test runs the whole flow against ZATCA's developer sandbox: onboarding, compliance checks, production certificate, issuing, reporting and clearance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/arkhan0072-dev/laravel-zatca" rel="noopener noreferrer"&gt;https://github.com/arkhan0072-dev/laravel-zatca&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Packagist: &lt;a href="https://packagist.org/packages/arkhan0072-dev/laravel-zatca" rel="noopener noreferrer"&gt;https://packagist.org/packages/arkhan0072-dev/laravel-zatca&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;ZATCA Wave 25 announcement: &lt;a href="https://zatca.gov.sa/en/MediaCenter/News/Pages/Wave25-E-invoicing.aspx" rel="noopener noreferrer"&gt;https://zatca.gov.sa/en/MediaCenter/News/Pages/Wave25-E-invoicing.aspx&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you are integrating ZATCA in Laravel, I would like to hear what your hardest part was.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>einvoicing</category>
      <category>opensource</category>
    </item>
    <item>
      <title>laravel-ledger: double-entry accounting for Laravel, enforced by the database</title>
      <dc:creator>Abdulrehman Khan</dc:creator>
      <pubDate>Fri, 27 Nov 2026 06:39:02 +0000</pubDate>
      <link>https://dev.to/arkhan0072/laravel-ledger-double-entry-accounting-for-laravel-enforced-by-the-database-1onl</link>
      <guid>https://dev.to/arkhan0072/laravel-ledger-double-entry-accounting-for-laravel-enforced-by-the-database-1onl</guid>
      <description>&lt;p&gt;If your Laravel app holds money for anyone (wallet balances, marketplace payouts, credits, refunds), sooner or later someone asks "why is this balance 40 dollars off?". The usual cause is a &lt;code&gt;balance&lt;/code&gt; column that something updated twice, or not at all.&lt;/p&gt;

&lt;p&gt;Accountants solved this centuries ago with double-entry bookkeeping: every movement of money is recorded as debits and credits that must be equal, and a balance is never stored, only added up. &lt;a href="https://github.com/arkhan0072-dev/laravel-ledger" rel="noopener noreferrer"&gt;laravel-ledger&lt;/a&gt; brings that to Laravel, and asks the database to enforce the rules instead of trusting every piece of code that touches the tables.&lt;/p&gt;

&lt;p&gt;This post shows how to use it, when it fits, and when another package is the better choice.&lt;/p&gt;

&lt;h2&gt;
  
  
  Install
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require arkhan0072-dev/laravel-ledger
php artisan migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It runs on PHP 8.3 and 8.4, Laravel 12 and 13, and MySQL, PostgreSQL or SQLite.&lt;/p&gt;

&lt;h2&gt;
  
  
  Your first entry
&lt;/h2&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Ledger&lt;/span&gt;\&lt;span class="nc"&gt;AccountType&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Ledger&lt;/span&gt;\&lt;span class="nc"&gt;Ledger&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Ledger&lt;/span&gt;\&lt;span class="nc"&gt;Line&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Ledger&lt;/span&gt;\&lt;span class="nc"&gt;Models&lt;/span&gt;\&lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$cash&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Account&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;'code'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'1000'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Cash'&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="nc"&gt;AccountType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Asset&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;     &lt;span class="s1"&gt;'currency'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'USD'&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;$sales&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Account&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;'code'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'4000'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'Sales'&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="nc"&gt;AccountType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Income&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="s1"&gt;'currency'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'USD'&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;$vat&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Account&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;'code'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'2100'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'VAT'&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="nc"&gt;AccountType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Liability&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'currency'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'USD'&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;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Ledger&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Invoice #1042 paid'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;    &lt;span class="c1"&gt;// $120.00, always integer cents&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sales&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$vat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2_000&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;$cash&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;balance&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;// 12000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An entry whose debits and credits differ, that mixes currencies, or that has fewer than two lines is refused before anything is written.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wallets that cannot be overdrawn
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$wallet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$user&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;ledgerAccount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Wallet'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccountType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nc"&gt;Liability&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'USD'&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;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;'min_balance'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;transfer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Order #12'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$sales&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1_800&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Refused if it would take the wallet below zero.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before checking the balance, the posting locks the account row (&lt;code&gt;SELECT ... FOR UPDATE&lt;/code&gt;), so two requests spending the last 10 dollars at the same moment cannot both succeed. Money coming in is never refused.&lt;/p&gt;

&lt;p&gt;Card-style holds work the same way:&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;$hold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;hold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$wallet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2_500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Ride 881'&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;addHour&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="c1"&gt;// reserve $25&lt;/span&gt;
&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;capture&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$hold&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$driverPayable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1_840&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                        &lt;span class="c1"&gt;// charge $18.40, free the rest&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;availableBalance&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;// excludes held money&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Webhooks that arrive twice
&lt;/h2&gt;

&lt;p&gt;Payment providers retry webhooks. Pass the provider's ID as an idempotency key and the second delivery returns the stored entry instead of booking the payment again:&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;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Stripe charge ch_3Pq'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'ch_3Pq'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Stripe charge ch_3Pq'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$lines&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'ch_3Pq'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// same entry, nothing new&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the same key comes back with different amounts, it is refused: that is a bug you want to hear about.&lt;/p&gt;

&lt;h2&gt;
  
  
  History nobody can rewrite
&lt;/h2&gt;

&lt;p&gt;Posted entries cannot be edited or deleted. A mistake is cancelled with a reversal entry, like on paper:&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;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$entry&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Customer cancelled'&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 not only enforced in Eloquent. A migration adds database triggers that refuse &lt;code&gt;UPDATE&lt;/code&gt; and &lt;code&gt;DELETE&lt;/code&gt; on posted entries in MySQL, PostgreSQL and SQLite, so raw SQL or another app sharing the database cannot rewrite history either. On PostgreSQL, a deferred constraint trigger also refuses to commit any entry that does not balance, whoever wrote it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reports
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;statement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// opening, running and closing balance&lt;/span&gt;
&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;incomeStatement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'USD'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     &lt;span class="c1"&gt;// profit and loss&lt;/span&gt;
&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;balanceSheet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'USD'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$asOf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// assets = liabilities + equity, always&lt;/span&gt;
&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;closeYear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'USD'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$yearEnd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$retainedEarnings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;closePeriod&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$monthEnd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// nothing can be posted into a closed month&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is also &lt;code&gt;php artisan ledger:verify&lt;/code&gt;, which audits the stored data for anything that bypassed the package and exits non-zero, so it can run in a scheduled job.&lt;/p&gt;

&lt;h2&gt;
  
  
  More than one currency
&lt;/h2&gt;

&lt;p&gt;Each entry balances in one currency. &lt;code&gt;convert()&lt;/code&gt; books an exchange as one entry per currency through exchange accounts, and &lt;code&gt;fxGainLoss()&lt;/code&gt; tells you, in your base currency and at today's rates, what your conversions gained or lost. &lt;code&gt;bookFxGainLoss()&lt;/code&gt; turns that figure into an entry when you close the month.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use it, and when not
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;Pick it when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;laravel-ledger&lt;/td&gt;
&lt;td&gt;You need a small, strict ledger inside your app (wallets, payments, marketplaces), in one or several currencies, with the database refusing bad data.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/ekmungai/eloquent-ifrs" rel="noopener noreferrer"&gt;eloquent-ifrs&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;You need full IFRS bookkeeping with invoices, VAT and statements out of the box. It is the most established option.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/scottlaurent/accounting" rel="noopener noreferrer"&gt;scottlaurent/accounting&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;You want a simple journal per model with a running balance and little else.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;a href="https://github.com/alimrun/laravel-ledger" rel="noopener noreferrer"&gt;alimrun/laravel-ledger&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;You want single-currency books with parties, periods and reports built in.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;laravel-ledger is young. It has a full test suite, but it has not yet run under years of production traffic the way eloquent-ifrs has. If you need a complete accounting product rather than a ledger inside your own app, start with eloquent-ifrs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selling in Saudi Arabia?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/arkhan0072-dev/laravel-zatca" rel="noopener noreferrer"&gt;laravel-zatca&lt;/a&gt;, from the same author, issues ZATCA (Fatoora) e-invoices. Each package works on its own; together, every legal invoice is also booked in your ledger exactly once:&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;$invoice&lt;/span&gt; &lt;span class="o"&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;Arkhan&lt;/span&gt;\&lt;span class="nc"&gt;Zatca&lt;/span&gt;\&lt;span class="nc"&gt;Zatca&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="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;issue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'pos-1'&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="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$ledger&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ZATCA invoice {$data['id']}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;debit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23_115&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$sales&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20_100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Line&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$vatPayable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3_015&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="n"&gt;idempotencyKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"zatca:{$invoice-&amp;gt;uuid}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reference&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$invoice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How it is tested
&lt;/h2&gt;

&lt;p&gt;Every feature was written test first. CI runs the suite on PHP 8.3 and 8.4, Laravel 12 and 13, and MySQL, PostgreSQL and SQLite (12 combinations), fails below 100% line coverage, and runs PHPStan at the maximum level.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/arkhan0072-dev/laravel-ledger" rel="noopener noreferrer"&gt;https://github.com/arkhan0072-dev/laravel-ledger&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Packagist: &lt;a href="https://packagist.org/packages/arkhan0072-dev/laravel-ledger" rel="noopener noreferrer"&gt;https://packagist.org/packages/arkhan0072-dev/laravel-ledger&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Releases and changelog: &lt;a href="https://github.com/arkhan0072-dev/laravel-ledger/releases" rel="noopener noreferrer"&gt;https://github.com/arkhan0072-dev/laravel-ledger/releases&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Issues and pull requests are welcome. If you try it on a real project, I would like to hear what was missing.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>fintech</category>
      <category>opensource</category>
    </item>
    <item>
      <title>When Should You Create Views in Your Database?</title>
      <dc:creator>Abdulrehman Khan</dc:creator>
      <pubDate>Sat, 21 Dec 2024 10:52:53 +0000</pubDate>
      <link>https://dev.to/arkhan0072/when-should-you-create-views-in-your-database-52hb</link>
      <guid>https://dev.to/arkhan0072/when-should-you-create-views-in-your-database-52hb</guid>
      <description>&lt;p&gt;Creating views in a database can offer significant performance improvements and simplifications, particularly when dealing with complex queries or aggregations. In this article, we'll explore when to use views and materialized views in your database, why they are useful, and how they can optimize your system's performance for faster results.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Database Views?
&lt;/h2&gt;

&lt;p&gt;A view in a database is a stored query that can be treated like a table. Views are not physical but virtual tables that provide an abstraction layer over your database schema. The query underlying a view is executed each time the view is queried, simplifying complex queries or aggregations.&lt;/p&gt;

&lt;p&gt;Materialized views are similar but differ in that they store the query results physically, updating periodically or on-demand, which can lead to significant performance benefits.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to Create Views in the Database
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Complex Joins and Aggregations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your queries involve multiple tables with complex joins and aggregations, creating a view can simplify the query structure and improve performance. For example, joining data from articles, authors, and tags can become tedious if repeated in every query. Instead, you can create a view that encapsulates the complex logic.&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

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

&lt;p&gt;Now, querying the view is simple and avoids repetitive joins:&lt;/p&gt;

&lt;p&gt;Example&lt;/p&gt;

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

&lt;p&gt;This is much faster than executing multiple joins every time you need this data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Frequent and Repetitive Queries&lt;/strong&gt;&lt;br&gt;
If your application runs the same complex query frequently (for example, generating monthly reports), a view can save time and reduce query complexity. By using a view, you avoid duplicating the same SQL code in different parts of the application.&lt;/p&gt;

&lt;p&gt;For example, suppose you're generating reports that frequently fetch the same dataset (like top articles by views or sales). In that case, you can create a view that predefines the logic, ensuring consistency and faster access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Encapsulation of Business Logic&lt;/strong&gt;&lt;br&gt;
Views can encapsulate business rules, complex filters, or data transformations that would otherwise need to be repeated throughout your application. This reduces the risk of errors and improves maintainability.&lt;/p&gt;

&lt;p&gt;Example: You could encapsulate business logic that combines user, article, and tag information in a single view, ensuring all parts of your application query the same logic without duplicating it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Performance Optimization (Materialized Views)&lt;/strong&gt;&lt;br&gt;
While regular views do not store data physically and thus do not inherently speed up queries, materialized views are a special case. Materialized views store the result of a query physically, so you can query them like a table, avoiding the need to recompute the result each time.&lt;/p&gt;

&lt;p&gt;This is especially useful for complex aggregations, reporting, or data warehousing scenarios.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; For a reporting system, instead of joining articles, users, and tags every time you generate a report, you could create a materialized view:&lt;/p&gt;

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

&lt;p&gt;Now, querying the materialized view is much faster than repeatedly executing the complex join queries.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;1. Simplifies Queries&lt;/strong&gt;&lt;br&gt;
Views simplify your SQL queries by encapsulating complex logic into a single object. Instead of repeatedly writing complex joins, aggregations, or business logic, you can query a view like a table. This makes your application code cleaner, more maintainable, and less error-prone.&lt;/p&gt;

&lt;p&gt;For instance, instead of manually joining articles, authors, and tags every time, you can just query the article_summary view to retrieve the same result with a much simpler query:&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Performance Boost (Materialized Views)
In cases where querying complex joins or aggregations is slow, materialized views can significantly reduce query time by storing the result of the query physically. This is particularly helpful when you need to perform operations like:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Data aggregation (e.g., summing or counting rows).&lt;br&gt;
Data reporting (e.g., generating monthly or yearly reports).&lt;br&gt;
Data transformation (e.g., applying complex filters or business rules).&lt;br&gt;
With materialized views, the query results are precomputed and stored. As a result, querying the materialized view is much faster than running the same complex query repeatedly.&lt;/p&gt;

&lt;p&gt;For example, a materialized view for a report that aggregates monthly article views would avoid the need to recompute the results each time:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;3. Reduces Repetition and Duplication&lt;/strong&gt;&lt;br&gt;
If multiple parts of your application need the same complex query logic, views allow you to centralize this logic. Instead of copying and pasting SQL code or using application-level logic to repeat complex joins or filters, you can create a single view that encapsulates the logic. This avoids duplication and makes your code easier to maintain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Improves Data Integrity&lt;/strong&gt;&lt;br&gt;
By abstracting complex logic in a view, you ensure that the same query logic is applied consistently across your application. This helps maintain data integrity and prevents discrepancies in how the data is queried or displayed. For example, if a calculation or transformation is part of the business logic, using a view ensures that it is always applied consistently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Provides Security and Access Control&lt;/strong&gt;&lt;br&gt;
Views allow you to abstract and control access to sensitive data. For example, you might want to expose certain data to different users but hide other sensitive information. You can create views that only expose certain columns or rows, thereby controlling what data is accessible.&lt;/p&gt;

&lt;p&gt;Example: You can create a view that exposes only non-sensitive user data:&lt;/p&gt;

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

&lt;p&gt;This way, users querying the view will not see sensitive columns such as passwords or credit card numbers.&lt;/p&gt;

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

&lt;p&gt;While views can optimize and simplify queries, they don’t automatically guarantee performance improvements. The actual performance benefits depend on how the view is used and whether it is a regular view or a materialized view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Regular Views
&lt;/h2&gt;

&lt;p&gt;No Performance Boost: Regular views don’t store data physically; they just store a query template. Every time you query a regular view, the underlying query is executed. This means complex queries using regular views may still take the same amount of time as the original query.&lt;br&gt;
Use for Simplification: Regular views are best used for simplifying queries and encapsulating business logic, but they may not always provide a performance boost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Materialized Views
&lt;/h2&gt;

&lt;p&gt;Precomputed Data: Materialized views store the result of the query and can significantly improve performance for complex reports, aggregations, or frequent read-heavy operations.&lt;br&gt;
Refresh Overhead: Materialized views need to be refreshed periodically to ensure they contain the latest data. This refresh process introduces some overhead, especially if the underlying data changes frequently.&lt;br&gt;
Space and Memory Usage: Materialized views consume storage space, and depending on the size of your dataset, they can increase disk usage.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Not to Use Views
&lt;/h2&gt;

&lt;p&gt;While views are useful in many scenarios, there are situations where you should avoid them:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Highly Dynamic Data:&lt;/strong&gt; If the underlying data changes frequently and the view needs to be refreshed often (in the case of materialized views), the overhead of maintaining the view might outweigh the performance benefits.&lt;br&gt;
&lt;strong&gt;Simple Queries:&lt;/strong&gt; If your query is simple and doesn’t involve complex joins, subqueries, or aggregations, creating a view may add unnecessary complexity to your database structure.&lt;br&gt;
Performance Degradation with Regular Views: Since regular views execute queries every time they are queried, they can degrade performance, especially when dealing with large datasets or complex queries. In such cases, it’s better to avoid views or use them selectively.&lt;/p&gt;

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

&lt;p&gt;Creating views in the database can be a powerful tool for optimizing query performance, simplifying complex queries, and ensuring consistency across your application. You should create views when you need to:&lt;/p&gt;

&lt;p&gt;Simplify complex joins, aggregations, or business logic.&lt;br&gt;
Eliminate repetitive query writing.&lt;br&gt;
Improve data security and control access to sensitive data.&lt;br&gt;
For performance-sensitive use cases, materialized views provide precomputed query results, reducing the need for repeated computations and significantly improving performance. However, be mindful of the refresh overhead and space requirements.&lt;/p&gt;

&lt;p&gt;Ultimately, use views strategically to enhance both the maintainability and performance of your database, ensuring fast and efficient results when working with large datasets or complex queries.&lt;/p&gt;

</description>
      <category>database</category>
      <category>laravel</category>
      <category>mysql</category>
      <category>databaseviews</category>
    </item>
    <item>
      <title>Multi tenancy connect with tenant DB without Initialization Or Dynamically</title>
      <dc:creator>Abdulrehman Khan</dc:creator>
      <pubDate>Thu, 03 Oct 2024 12:41:15 +0000</pubDate>
      <link>https://dev.to/arkhan0072/multi-tenancy-connect-with-tenant-db-without-initialization-or-dynamically-2hco</link>
      <guid>https://dev.to/arkhan0072/multi-tenancy-connect-with-tenant-db-without-initialization-or-dynamically-2hco</guid>
      <description>&lt;p&gt;&lt;strong&gt;Multi-Tenancy Without Initialization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a typical multi-tenancy setup, packages like Stancl/Tenancy are used to initialize tenant context by switching configurations automatically, including the database connection. This involves loading a tenant's specific configuration (like database credentials) after "initializing" the tenancy environment. However, in this case, we're avoiding explicit tenancy initialization and manually connecting to the tenant's database dynamically.&lt;/p&gt;

&lt;p&gt;Follow the Steps&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Retrieving Tenant Data&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;The code starts by retrieving the Tenant record from the central database using the sport_id. This allows us to extract tenant-specific details, such as the database name.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error Handling for Tenant Existence&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;The system ensures that if the Tenant cannot be found using the provided sport_id, it returns an error message to prevent any further processing, ensuring graceful failure handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dynamically Setting Tenant Database Connection&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;Here's where the core dynamic connection logic happens:&lt;br&gt;
Manual Configuration: Laravel's config() helper is used to set a new database connection (tenant). This configuration is created on the fly based on the tenant’s database name ($tenantDBName) and uses environment variables for common settings like host, username, and password.&lt;br&gt;
This essentially creates a new connection for this request without altering the default connection defined in Laravel's database.php config file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Tenant Connection&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;on('tenant'): The method .on('tenant') explicitly tells Laravel's Eloquent ORM to use the dynamically created tenant database connection (tenant). This ensures that the queries for fetching the article and its relationships are run against the tenant’s specific database, not the central one.&lt;br&gt;
Eager Loading: The code uses Eloquent's with() method to eager load related models like subjects, image, and author. This allows for more efficient database queries by reducing the number of individual queries that would otherwise be required to retrieve these relationships.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error Handling for Article (or your Model) Retrieval&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;In the event that the article cannot be found in the tenant’s database, the system gracefully handles the error by redirecting back with an appropriate message.&lt;/p&gt;

&lt;p&gt;Few more things to consider and understand ...&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages of This Approach&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No Global Tenancy Initialization:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In certain scenarios, initializing a global tenancy environment across requests can be resource-heavy or unnecessary, especially if all you need is to perform isolated operations on a tenant’s database. This approach allows you to connect directly to the tenant’s database when needed, without initializing tenancy for the entire application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability and Flexibility:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By setting database connections dynamically on a per-request basis, the application is more flexible in handling a large number of tenants. Each tenant’s connection is isolated and only created when necessary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Use Case:&lt;/strong&gt;&lt;br&gt;
This approach is particularly useful in applications where you have different databases for tenants but share most other resources, like codebase, environment settings, or other global configurations. This technique avoids the overhead of multi-tenant packages but still provides isolation for database-related operations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limitations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Database Connections Per Request:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every time this method is called, it dynamically sets a new connection. While efficient for small-scale applications, this could become resource-intensive for a high number of concurrent users or tenants. Managing the lifecycle of these connections should be done carefully.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The above code effectively handles dynamic multi-tenancy by connecting to a tenant’s database without using a tenancy package or initializing a global tenancy context. This approach offers flexibility, especially for applications where only certain operations need to be tenant-aware, and avoids the complexity and overhead of managing tenancy at a global level.&lt;/p&gt;

&lt;p&gt;It's an elegant solution for applications with isolated tenant databases but shared application logic and infrastructure, aligning well with Laravel's philosophy of simplicity and flexibility.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>multitenancy</category>
      <category>webcomponents</category>
      <category>laravel</category>
    </item>
    <item>
      <title>How to Setup Multi Tenant App in minutes with Laravel PART 2</title>
      <dc:creator>Abdulrehman Khan</dc:creator>
      <pubDate>Thu, 05 Sep 2024 07:53:21 +0000</pubDate>
      <link>https://dev.to/arkhan0072/how-to-setup-multi-tenant-app-in-minutes-with-laravel-part-2-5560</link>
      <guid>https://dev.to/arkhan0072/how-to-setup-multi-tenant-app-in-minutes-with-laravel-part-2-5560</guid>
      <description>&lt;p&gt;&lt;strong&gt;Now your first multitenancy app is ready to go live and you can test on local but for that you need to create a TenantController from where you can register the tenants&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;this will be basic code you can modify it for more stuff as per your requirements&lt;/p&gt;

&lt;p&gt;now you will need to modify your route service provider if you havent set it accordingly &lt;/p&gt;

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

&lt;p&gt;now you can set you web.php and tenant.php file accordingly &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;tenant.php&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;Now your app is up and ruining please make modification as per your requirements &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>saas</category>
      <category>php</category>
      <category>backend</category>
    </item>
    <item>
      <title>How to Setup Multi Tenant App in minutes with Laravel</title>
      <dc:creator>Abdulrehman Khan</dc:creator>
      <pubDate>Mon, 06 Nov 2023 10:53:51 +0000</pubDate>
      <link>https://dev.to/arkhan0072/how-to-setup-multi-tenant-app-in-minutes-with-laravel-5d0d</link>
      <guid>https://dev.to/arkhan0072/how-to-setup-multi-tenant-app-in-minutes-with-laravel-5d0d</guid>
      <description>&lt;p&gt;In this article you will learn how to setup the basic Laravel multi tenant app in minutes with some simple steps.&lt;/p&gt;

&lt;p&gt;Multi tenancy is a good concepts for new SaaS based Applications it will help you to keep the one codebase for multiple domains and each domain contains its own data.&lt;/p&gt;

&lt;p&gt;Let get Started!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a new Laravel Project&lt;/li&gt;
&lt;li&gt;Install/setup Laravel Breeze&lt;/li&gt;
&lt;li&gt;Install multitenancy package with following command
&lt;/li&gt;
&lt;/ol&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;After it completes run the following command
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan tenancy:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;migrations&lt;br&gt;
a config file (config/tenancy.php),&lt;br&gt;
a routes file (routes/tenant.php),&lt;br&gt;
and a service provider file &lt;br&gt;
app/Providers/TenancyServiceProvider.php&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Recap&lt;br&gt;
What we have done so far&lt;br&gt;
a. create a new project&lt;br&gt;
b. install tenancy package&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step for multiple domains and database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once the above steps are done we need to register our tenancy service provider in the following file&lt;br&gt;
Then add the service provider to your config/app.php file:&lt;/p&gt;

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

&lt;p&gt;Now we need to create a Tenant Model for that we will use following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;After that a model will Created in side the App/Models/ we need to change the code with following later we will update it more according to our needs&lt;/p&gt;

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

&lt;p&gt;by default it will use the package Tenant Model but we want the system to use our model that we have just created for that we have to go in the config folder/directory and then find the tenancy.php file and make the following changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'tenant_model' =&amp;gt; Tenant::class,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;change the Above line of code with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'tenant_model' =&amp;gt; \App\Models\Tenant::class,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;If you don't need domains or databases, ignore the steps above. Everything will work just as well.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can create tenants like any other models:&lt;/p&gt;

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

&lt;p&gt;You may register central routes in routes/web.php or routes/api.php like you're used to. However, you need to make one small change to your RouteServiceProvider.&lt;br&gt;
You don't want central routes — think landing pages and sign up forms — to be accessible on tenant domains. For that reason, register them in such a way that they're only accessible on your central domains.Now we need to set the routes for the tenants and central app&lt;br&gt;
let go the the RouteServiceProvider.php and replace the code with following code from boot function&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Tenant routes&lt;/strong&gt;&lt;br&gt;
You may register tenant routes in routes/tenant.php file&lt;br&gt;
by default it will look like this&lt;/p&gt;

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

&lt;p&gt;Now we need to create copy the migration files from migration folder to the tenant folder that resides in migration folder and copy the required migration files&lt;/p&gt;

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

&lt;p&gt;once this done we need to create a job that will insert the data in tenant table and also generate the tenant database and insert that record in it&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;and replace the file code with the code bellow&lt;/p&gt;

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

&lt;p&gt;what this file do is it will always run as soon as you will hit the create tenant function and once it will be execute it will run the migration and insert the first record for you so you can login and access your tenant application&lt;/p&gt;

&lt;p&gt;but still we have to do some more fix's to get the working tenant application&lt;/p&gt;

&lt;p&gt;now we need to do some modifications in TenantServiceProvider.php &lt;br&gt;
do the following changes&lt;/p&gt;

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

&lt;p&gt;uncomment the JobSeedDatabase and add our created job bellow that \App\Jobs\SeedTenantJob::class&lt;/p&gt;

&lt;p&gt;like mentioned in the picture. Remember that order is very important in this file if you put our job at top and JobSeedDatabase bellow that it will throw error as you need to first run the migrations then insert the data our job part is for the insertion of data and JobSeedDatabase is to run the migrations&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now your first multitenancy app is ready to go live and you can test on local but for that you need to create a TenantController from where you can register the tenants&lt;/strong&gt;&lt;/p&gt;

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

&lt;p&gt;this will be basic code you can modify it for more stuff as per your requirements&lt;/p&gt;

&lt;p&gt;now you will need to modify your route service provider if you havent set it accordingly &lt;/p&gt;

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

&lt;p&gt;now you can set you web.php and tenant.php file accordingly &lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;updated tenant.php&lt;/strong&gt;&lt;/p&gt;

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

</description>
      <category>laravel</category>
      <category>multitenancy</category>
      <category>laraveltenant</category>
      <category>saas</category>
    </item>
  </channel>
</rss>
