Scroll to top

Use DB Class more than your Models

You may be thinking wow its so easy so i call Posts::find(1); If you dont need to dont use it . Below are some situations when you use models

1.When you need relationships

2.Scope methods

3.Putting some helper methods in your models

If above situation is negative then please use DB::table(‘posts’); since it is a way more faster than Eloquent Method .


We have inserted 1000 rows in the database using DB class and Eloquent Model

1. Eloquent Model took 56.85 sec

2. DB class took 35.35 sec

So before you start creating models for all of your tables think again and divide them into Eloquent models and tables which can be directly used with DB class.

Eloquent Posts Model

        $t=new Posts();
        $t->name=$i." Row";

DB Class

        DB::table("posts")->insert(["name"=>$i." Row"]);
Author avatar

Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Post a Comment

Your email address will not be published. Required fields are marked *