17 Laravel Eloquent Tips and Tricks

608
laravel-eloquent-tips

According to Laravel, Eloquent is an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding “Model” that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.

In this article, we will learn about Laravel eloquent tips and tricks.

Never update column

In laravel, we can set the column to never update after the initial creation. For this, we need to use mutator on model.

class User extends Model
{
    public function setUuidAttribute($value)
    {
        if ($this->uuid) {
            return;
        }
        
        $this->attributes['uuid'] = $value;
    }
}

Hide columns

Using an eloquent model query, we can hide any column from the query result. For this, we need to use makeHidden method on the modal query.

$users = User::find([1,2,3,4,5])->makeHidden(['created_at', 'deleted_at']);

OrderBy on Eloquent relationships

We can order the records based on the relationships column in Laravel. This can be done in various ways but we can simply use orderBy in the relationships as in the example below.

<?php
class User
{
    public function comments()
    {
        return $this->hasMany('Comment');
    }
}

class Controller
{
    public function index()
    {
        $column = Input::get('orderBy', 'defaultColumn');
        $comments = User::find(1)->comments()->orderBy($column)->get();

        // use $comments in the template
    }
}

Where Date Methods

In eloquent, we can compare date using where date methods. Laravel provides methods such as whereDate(), whereDay()​, ​whereMonth()​, ​whereYear()​,​ and ​whereTime()​.

The whereDate check for the date, whereDay checks for the day of the month, whereMonth checks for the month and so on.

$articles = Article::whereDate('created_at', '2018-01-31')->get();
$articles = Article::whereMonth('created_at', '12')->get();
$articles = Article::whereDay('created_at', '31')->get();
$articles = Article::whereYear('created_at', date('Y'))->get();
$articles = Article::whereTime('created_at', '=', '14:13:58')->get();

Increment & Decrement

From Laravel 5.7+ you can increment or decrement the given column by using increment() or decrement() methods without writing the manual update statement. If you want to increment some DB column in some table, just use ​increment()​ function. Also, we can increment not only by 1, but also by some number, like 50.

DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);

DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);

Disable Timestamps in Database

We can disable the timestamps i.e. created_at and updated_atin Laravel. For this, we can remove the $table->timestamps()which will disable the timestamps in database. However, we can also set the timestamps to false to ignore the already created columns.

<?php

class Article extends Model
{
    public $timestamps = false;
}

Order Migration Files

In Laravel, we can when we create migration files, a timestamp is automatically added as a prefix to the file name. This timestamp is used to order migrations files. We can change the timestamp to change the order of the migrations.

2018_08_04_070443_create_articles_table.php​ 
To
2018_07_04_070443_create_articles_table.php

Soft Delete and Restore

Laravel has an interesting feature known as soft delete. Soft Delete means that you are flagging a record as deleted in a particular table, instead of actually being deleting the record. We can use $table->softDeletes()to add soft delete to each model.

When using soft-deletes, you can restore multiple rows in one sentence.

Article::withTrashed()->where('author_id', 1)->restore();

You can learn in dept about Soft Delete in this article.

Specific Has Many Records

In Eloquent ​hasMany()​ relationships, you can filter out records that have X amount of children
records.

// Author -> hasMany(Book::class)
$authors = Author::has('books', '>', 5)->get();

Select Specific Columns during Query

We can select specific columns when querying the database. We can achieve this while querying all records or paginated records.

User::all(['name', 'email']);

User::where('id', 1)->get(['name','email']);

User::find($id, ['name', 'email']);

User::where('age', 20)->pluck('name', 'email')->all();

Column name change

In Eloquent Query Builder, we can specify “as” to return any column with a different name, just
like in plain SQL query.

$users = DB::table('users')
    ->select('name', 'email as user_email')
    ->get();

Default Model

We can assign a default model in ​belongsTo​ relationship, to avoid fatal errors when calling it
like ​{{ $post->user->name }}​ if ​$post->user​ doesn’t exist.

/**
* Get the author of the post.
*/
public function user()
{
    return $this->belongsTo('App\User')->withDefault();
}

Use hasMany to create Many

If you have ​hasMany()​ relationship, you can use ​saveMany()​ to save multiple “child” entries
from your “parent” object, all in one sentence.

$post = Post::find(1);
$post->comments()->saveMany([
    new Comment(['message' => 'First comment']),
    new Comment(['message' => 'Second comment']),
]);

Eager Loading with Specific Columns

We can do Laravel Eager Loading and specify the exact columns we want to get from the relationship. Below will get only id and name of author table.

$users = App\Book::with('author:id,name')->get();


We can do that even in deeper, second level relationships:

$users = App\Book::with('author.country:id,name')->get();

Touch parent updated_at easily

If you are updating a record and want to update the ​updated_at​ column of parent relationship
(like, you add new post comment and want ​posts.updated_at​ to renew), just use ​$touches
= ['post'];​
property on child model.

class Comment extends Model
{
    /**
    * All of the relationships to be touched.
    *
    * @var array
    */
    protected $touches = ['post'];
}

Use withCount() to Calculate Child Relationships Records

If you have ​hasMany()​ relationship, and you want to calculate “children” entries, don’t
write a special query. For example, if you have posts and comments on your User
model, write this ​withCount()​:

public function index()
{
    $users = User::withCount(['posts', 'comments'])->get();
    return view('users', compact('users'));
}

You can learn more about this in this article.

Quick Order by created_at

Instead of:

User::orderBy('created_at', 'desc')->get();

You can do it quicker:

User::latest()->get();

By default, ​latest()​ will order by created_at​. There is an opposite method ​oldest() ​which would order by created_at ascending.

User::oldest()->get();

Also, you can specify another column to order by. For example, if you want to use
updated_at​, you can do this:

$lastUpdatedUser = User::newest('updated_at')->first();

You can learn more about this in this article.

This is all about eloquent tips and tricks. This article is inspired by laraveldaily.com which is an amazing blog. Don’t forget to drop your thoughts in the comment section below.

Read More Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.