0
votes

Whenever I make a laravel project, I always define relationships not just in the model but also in the database (I always do a php artisan make:model ModelName -mcr). Sometimes I saw code that they only define relationships in model but not in the database and vice versa. Can someone tell me what are their differences and should I always define relationships in the model and in the database or should I do in one of them? Also, I always use both laravel eloquent queries and laravel query builder which is DB::table. What are the pros and cons of using both? Which are faster? Tell me your opinions and advice. I hope you get my point. Thanks

Sample Model

public function damage(){
        return $this->hasMany('App\Product');
    }

Sample Table


        Schema::table('damages', function($table)
        {
            $table->foreign('product_id')
                    ->references('id')
                    ->on('products')
                    ->onDelete('cascade');
        });

Sample query

  public function getDamages(){
        $damages = DB::select(
            "SELECT damages.product_id, damages.quantity, damages.price, products.product_name
            FROM damages
            JOIN products on damages.product_id = products.id"
           );
        return view('damages', compact('damages'));

//or

 $recipes = DB::table('recipes')
            ->join('category_recipe', 'recipes.id', '=', 'category_recipe.recipe_id')
            ->join('category', 'category.id', '=', 'category_recipe.category_id')
            ->join('users', 'users.id', '=', 'recipes.user_id')
            ->where('category.id', '=', $cat_id)->get(array('recipes.*','users.*'));  

    }

Sample query 2

public function index(){
       $damage = Damage::all();
//or
       $suppliers = Supplier::all()->where('status', 'active');
//or
       $recipes = Recipe::with('category')->where('category_id',$cat_id)->get();  

}
1
isnt that two questions..? you should define relationship in table or laravel migration to keep database consistency. both eloquent and query builder is usable and safe (as long as you stay away from using DB::raw with string concatenation, use proper binding). - Bagus Tesa
why should i stay away from using DB::raw sir? Is that bad? - draw134
as stated in the laravel documentation, be wary of DB::raw as you might accidentally allow sql injection on your website, which is bad. - Bagus Tesa
ohh i see sir. thanks for the info. - draw134

1 Answers

2
votes

Based on this article : https://kursuswebprogramming.com/perbedaan-eloquent-dan-query-builder-laravel/

Eloquent ORM is an extended method based on Query builder. It developed to make a simple source code, make and you faster to write your code. But for me it's so less expressif cause you have to set your model name as table name.

And also, there is an comparison about execution time :

Eloquent ORM (Execution time : 1.41 s, Queries Executed : 1000)

<?php

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

Query Builder (Execution time : 938 ms, Queries Executed : 1000)

<?php
Route::get("test",function(){
    for($i=0;$i<1000;$i++){
         DB::table("countries")->insert(["label"=>$i." Row"]);
    }
});
?>

This is a prove Query Builder is 0.5 s faster than Eloquent ORM.