DEV3 Technologies home

Laravel database snippets

Middleware, policies, jobs, services and migrations. 12 free snippets, each with an explanation and usage notes.
12
snippets
2
types

Database snippets

Laravel · DatabaseYearly invoice numbering sequenceGap-free invoice numbers such as INV-2026-000042 using one locked counter row per year and prefix, safe under concurrent requests.invoicesequencetransactions Laravel · DatabaseVendor payout ledgerAn append-only ledger where sales, commissions and payouts are signed entries, so a vendor's balance is always a sum and never a stored guess.ledgerpayoutsmarketplace Laravel · DatabaseTeam-scoped global scope traitA BelongsToTeam trait that filters every query by the current team and stamps new rows automatically, failing closed when no team is set.multi-tenantglobal-scopeeloquent Laravel · DatabaseSplit an order per vendorGroup cart lines by vendor, create one sub-order each and share the order discount across them using integer largest-remainder allocation.marketplaceordersdiscount Laravel · DatabaseNearest-rider assignmentFind the closest available courier with a bounding-box prefilter and a haversine distance, then claim them atomically for a delivery.geodispatchhaversine Laravel · DatabaseMonthly usage metering with automatic resetCount usage per team and metric in one row per month, consuming units with a single conditional update so a limit can never be overshot.meteringquotausage Laravel · DatabaseTamper-evident document audit trailAppend-only audit events where each row stores a hash of the previous one, with a verify method that detects edited or deleted history.audithash-chaineloquent Laravel · DatabaseDelivery-slot capacity bookingReserve a delivery time slot with a single conditional UPDATE that cannot oversell, plus a matching release when an order is cancelled.bookingcapacityatomic Laravel · DatabaseBid placement with row lockPlace an auction bid inside a transaction that locks the auction row, validates the minimum increment and updates the leader atomically.auctiontransactionslocking Laravel · DatabaseAtomic donation goal progressUpdate a campaign's raised total with a single SQL increment and fire a goal-reached event exactly once, even under concurrent donations.donationsatomicprogress Laravel · DatabaseApplicant pipeline state machineA backed enum with an allowed-transitions map and a locked mover that enforces rules, requires a reason for rejection and writes a history row.state-machineenumworkflow Laravel · DatabaseAppend-only points ledgerRecord loyalty points as immutable ledger rows with a running balance, an idempotency key and a locked member row, so totals can always be audited.ledgerloyaltypoints