eloquent

Laravel soft deletes for data retention

Soft deletes mark records as deleted without removing them from the database, enabling recovery and audit trails. I add SoftDeletes trait to models and a deleted_at timestamp column. Calling delete() sets deleted_at instead of removing rows. Soft-dele

Laravel Eloquent relationships with eager loading

Eloquent ORM makes working with database relationships intuitive and powerful. I define relationships using methods like belongsTo, hasMany, and belongsToMany that return query builders. The beauty of Eloquent is lazy loading—relationships load only w

Laravel scopes for reusable query logic

Query scopes encapsulate common query constraints into reusable methods on models. Local scopes prefix methods with scope and accept a query builder plus additional parameters. I chain scopes fluently—Post::published()->featured()->get(). Global

Laravel model observers for lifecycle hooks

Observers consolidate model event listeners in dedicated classes, preventing bloated models. Each observer method corresponds to an Eloquent event—creating, created, updating, updated, deleting, deleted. I register observers in boot() methods of servi

Laravel accessors and mutators for attribute handling

Accessors and mutators transform model attributes when retrieving or setting values. Accessors format data for presentation—converting cents to dollars, concatenating names, or generating computed properties. Mutators normalize input—hashing passwords

Laravel pagination with custom views

Pagination divides large datasets into pages, improving performance and UX. Eloquent's paginate() method returns a paginator with data and metadata. The links() method renders pagination UI. I customize per-page counts with paginate(50). Simple pagina