Use DB Class more than your Models

[norebro_social_networks icon_layout=”boxed”]

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 .

Test:

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

Route::get("test",function(){
    for($i=0;$i<1000;$i++){
        $t=new Posts();
        $t->name=$i." Row";
        $t->save();
    }
});

DB Class

Route::get("test",function(){
    for($i=0;$i<1000;$i++){
        DB::table("posts")->insert(["name"=>$i." Row"]);
    }
});
https://www.kodeinfo.com
Comments to: Use DB Class more than your Models

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

    Attach images - Only PNG, JPG, JPEG and GIF are supported.

    Stay connected

    Categories

    Newsletter

    Tags

    Recent Comments

      Recent Posts

      Recent Comments

        Archives

        Categories

        Meta