Categories: Laravel Framework - The PHP Framework For Web Artisans, Laravel Tutorial: Step by Step Guide to Building Your First Laravel Application - Techanical-Atom

Laravel Eloquent Awesome Tips That You Need To Know3 min read

Laravel Eloquent Awesome Tips That You Need To Know
Reading Time: 2 minutes

Laravel Eloquent ORM which is simple ActiveRecord implementation for working with your database. Each database table in the Laravel has a corresponding “Model” using which it can interact with the corresponding table of the model. But there are lots of hidden or as i can say that semi-hidden tips and tricks that everyone should know and can implement in their next project or even in there on going project. So not wasting more time lets dive direct into the article and some of these tricks of Laravel Eloquent.

Laravel Eloquent Awesome Tips That You Need To Know
Laravel Eloquent Awesome Tips That You Need To Know

Awesome Tips Of Laravel Eloquent That You Need To Know

# Example 1 The XorY methods

There are many functions in Laravel where you have to do certain thing otherwise do another thing. For example :- Do task A otherwise do the Y task. Now to implement this we used to do the following.

But instead of this what we can do is the following  with the findOrFail()

 #Example 2 Multiple find() enteries

Everyone knows and uses find() function to find any record in the database but very few people know about that we can find multiple enteries using the find(). Interesting isn’t it. Let see how.

Following query will find the records with the ids 1 , 2 and 3.

# Example 3 Evergreen whereX method

Almost everyone know about the where mehtod which we use with the query but there is whereX mehtod as well using which we can modify our query something like this.

In the above example Email is the name of the column we are applying where clause. The above query will search the database the column email with value 1. Isn’t it cool?

# Example 4 Replicate() : Make a copy of a row

The name of this is self explanatory. If you want to make copy of one of your database row then simply use this replicate as shown below.

Following query will find the row with the id 1 and then will replicate that line of the database.

Read More :- PHP vs Python In 2018 – Which Is Better For Beginners

# Example 5 Multiple parameters with the orWhere method.

Sometime we have multiple orWhere clauses in a single query so instead of making a long chain of the orWhere method what we can do is make an array and enter our values into the array like the following.

following query will work in the same as you normally write but it will be more efficient in the long run.

These few gems that i normally use but i ma sure there will be more of such awesome Laravel Eloquent tips and tricks that you can easily use in one of your project. If you also know any of awesome Eloquent tips then do post them in the comment section below. If you like this article then do comment , follow or even share this article.

Cheers ! Happy Coding

All Rights Reserved @ Techanical-Atom
%d bloggers like this: