Home Eloquent Methods You Probably Didn't Know Exists

Enter a search term to find articles.
eloquent-methods-you-probably-didnt-know-exists
2020-07-28
1845

Eloquent Methods You Probably Didn't Know Exists

Eloquent ORM is probably the most appreciated features of Laravel. It enables programmers to interact with the database in a fluent manner. And just like any other features in Laravel, it has couple hidden gems that you probably never knew existed.

In this article, let's take a look at some of these hidden gems that I personally use quite so often.

whereKey / whereKeyNot

That's right! These are Eloquent methods are specific for querying a model by its primary key. The obvious benefit of this is that you, as the programmer, don't need to remember the name of the model's primary key.

// Instead of doing...
Post::whereIn('id', [1,2,3])->get();
Post::whereNotIn('id', [1,2,3])->get();

// You can do...
Post::whereKey([1,2,3])->get();
Post::whereKeyNot([1,2,3])->get();

qualifyColumn

You can use this method if you want to prepend your columns with the table name. Really helpful for long queries with nested conditions.

$sql = Post::where(function ($query) {
        $query->whereNotNull($query->qualifyColumn('published_at'));
})->toSql();

dd($sql);

// select * from `posts` where (`posts`.`published_at` is not null) and `posts`.`deleted_at` is null

getQualifiedKeyName

This method simply returns the name of the primary key of the model with the table name.

(new \App\Models\Post)->getQualifiedKeyName(); // Will return 'posts.id'
(new \App\Models\Post)->getKeyName(); // Will return 'id'

increment / decrement

Often times we have a column on a model that we need to manually increment/decrement. So instead of using the "update" method, we can use "increment / decrement" instead.

Also, increment and decrement methods both accepts 2nd parameter as the amount of increment, and 3rd parameter as an array of extra columns you wish to update.

when / unless

If you ever come across a situation wherein your queries are based on a condition then these methods might be perfect for your use-case.

You can use when if you want to apply a callback to an Eloquent query when a certain condition is true.

Post::when($someBoolean, function () {
    // This call back will run if $someBoolean is true...
});

You can use unless if you want to apply a callback to an Eloquent query when a certain condition is false.

Post::unless($someBoolean, function () {
    // This call back will run if $someBoolean is false...
});

You can also pass a third parameter to both methods as a default callback.

And both methods are also chainable to other Eloquent methods!

loadMissing

This method is almost the same as the load method. But as the name implies, it will only load relationship that has not yet been loaded on the current model. This means performance savings when lazy loading relationships.

The magic where

When doing where conditions on your queries, you can just append the name of the column to the where method in camel-case format, like so:

$sql = User::whereEmail('john@doe.com')->toSql();

dd($sql);

// select * from `users` where `email` = ?

Hopefully, you learned something from this article and you'll be able to utilize these methods.

Don't forget to visit the official Laravel documentation for more info:

https://laravel.com/docs/7.x/eloquent

Cheers!

Marvin Quezon

Full Stack Web Developer
Marvin Quezon · Copyright © 2024 · Privacy · Sitemap