Tag articles, products, and anything else through a single reusable table using a polymorphic many-to-many relationship.
#Eloquent
Articles
Stop stuffing regex into strings. Build reusable, testable validation rules with invokable Rule objects and closures.
Calling ->get() on a giant table loads every row into memory at once. Laravel gives you chunk, chunkById, cursor, and lazy to process big result sets safely.
Who changed this record, and when? Build a reusable audit trail using Eloquent observers so every create, update, and delete is logged automatically.
Storing money as a float invites rounding bugs. Build a custom Eloquent cast that keeps cents as integers in the database but exposes a clean value object.
Wrapping related writes in a transaction is easy to get subtly wrong. Laravel's DB::transaction() closure handles commit, rollback, and deadlock retries for you.
Stop wrapping query builder calls in if statements. The when() and unless() methods let you apply constraints conditionally while keeping the chain fluent.
Eloquent will happily hide an N+1 query until it hits production. Strict model mode turns those silent mistakes into loud exceptions during development.
Factory states, relationships, and sequences let you express exactly the test data a scenario needs in a single readable line.
Cast Eloquent attributes to backed PHP enums to replace magic strings with real types, and give your enums behavior with helper methods.
Build a shared-database multi-tenant Laravel app from scratch using a tenant column, middleware, and a global scope that keeps every query isolated.
Query scopes let you name and reuse common Eloquent constraints so your controllers stay readable and your query logic lives in one place.
Eight practical Laravel patterns that make your code read more like intent and less like implementation — from fluent helpers to conditional chains.
Route model binding does more than inject a model. Custom keys, scoped bindings, soft-delete handling, and enums all hide behind this everyday feature.
These five underused Eloquent features will help you write less code, run fewer queries, and make your models a lot more pleasant to work with.