Eloquent ORM

It is object relational mapper that provides smooth interaction with database.
 

How to use whereHas to retrieve rows based on existence of relationship

whereHas() method is used to add further query constraints on top of has() method. Suppose,  there is user hasMany relationship...


Using withAggregates in Eloquent way

Suppose Post hasMany Rating and there is a field rating in ratings table. For this following snippet can be used...


Selecting fields inside eager loading method "with"

Suppose there is a relation Post belongsTo User. If we are retrieving posts with associated user relation, we can use...


Working with different table name for a model

If we need to work with a model with different table name which is usually needed for old projects where...


changing auto incremeneting field names

Like different table names it is also usually needed for old projects where incrementing field names for a table could...


Changing pagination setup mentioning item amount per page from model

By default it is setup to 15 in laravel but you can do that from model as well. You do...


Changing timestamp field names

Suppose there are timestamps fields created and modified in table then if we were to use that in project we...


Adding custom date fields for processing with Carbon object

Sometimes besides created_at and updated_at we may need another custom date fields which can be needed to process with Carbon...


Setting up custom date format

By default it is defined as 'Y-m-d H:i:s' and in order to use different date format we can use following...


Using fillable or guarded

If you have Post Model with title and description you can setup these fields with fillable or gurded in order...