Overview
Dashboard Laravel uses Eloquent ORM for database interactions. Eloquent provides an elegant, simple ActiveRecord implementation for working with your database, where each database table has a corresponding “Model” used to interact with that table.Available Models
The application includes the following Eloquent models:App\Models\User- User authenticationApp\Models\Cliente- Client/customer managementApp\Models\Venta- Sales transactionsApp\Models\Factura- Invoice managementApp\Models\Mensaje- Client messagingApp\Models\Estadisticas- Analytics dataApp\Models\Home- Homepage contentApp\Models\Nosotros- About us information
Model Relationships
Cliente Model
The Cliente model demonstrates one-to-many relationships:Venta Model
The Venta model shows both belongs-to and has-one relationships:Factura Model
The Factura model with date casting:Mensaje Model
The Mensaje model with boolean casting:CRUD Operations
Creating Records
- Single Record
- With Relationships
Reading Records
- Basic Queries
- With Relationships
- Advanced Queries
Updating Records
- Single Record
- Multiple Records
Deleting Records
- Single Delete
- Bulk Delete
Query Scopes
Create reusable query scopes in your models:Relationship Queries
Querying Relationships
Eager Loading
Accessors and Mutators
Add custom attributes to your models:Model Events
Hook into model lifecycle events:Best Practices
Eloquent Best Practices
Eloquent Best Practices
- Use mass assignment protection - Always define
$fillableor$guarded - Eager load relationships - Avoid N+1 query problems with
with() - Use query scopes - Create reusable query logic
- Leverage relationship methods - Use
create()on relationships instead of manual foreign key assignment - Cast attributes - Define
$castsfor dates, booleans, JSON, etc. - Use transactions - Wrap multiple operations in database transactions
- Index foreign keys - Ensure foreign key columns are indexed for performance
