Hasan Setiawan

Write, write, write give your wings on code!

Follow me on GitHub

Whether you need to use Laravel Eloquent or Query builder

Just to be clear, I am not going to compare between Laravel eloquent and Query Builder. For me both of them are good option and you can doing work with them. Laravel eloquent give you benefit on getting data without writing complex query.

Also Eloquent has ton of extra features the query builder lacks, such readability, accessors, mutators, JSON/Array conversion, hiding sensitive attributes, automatic timestams, automatic attribute casting, sofdeletes. And one thing, by using eloquent you will feel more comfortable on switching your DB engine. Because you don't need to rewrite your code. But ya with notice since your DB engine are supported by eloquent you are save.

              
// Snippet code of Query Builder
public static function getBreakdown($date)
{
    $previousSix = previousSixHelper($date);

    $dinein = DB::select("
      select DISTINCT patron_type, count(total_amount) as total_patron from la_order_summary
      where order_type = '4' and left(order_datetime, 10) BETWEEN :previousSix and :dateParam
      ", ['previousSix' => $previousSix,
      'dateParam' => $date,
    ])[0];

    $takeAway = DB::select("
      select DISTINCT patron_type, count(total_amount) as total_patron from la_order_summary
      where order_type = '5' and left(order_datetime, 10) BETWEEN :previousSix and :dateParam
      ", [
      'previousSix' => $previousSix,
      'dateParam' => $date,
    ])[0];

    return [
      'dinein' => $dinein->total_patron,
      'takeAway' => $takeAway->total_patron,
    ];
}



// Snippet code of Laravel Eloquent
/**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function descriptions()
{
  return $this->hasMany('App\Addon_Description', 'addon_id', 'id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function products()
{
  return $this->belongsToMany('App\Product', 'product_addon')
    ->withTimestamps();
}
              
            

Eloquent is Laravel's implementation of Active Record pattern and it comes with all its strengths and weaknesses. It is a good solution to use when you process a single entity in a CRUD manner - that is, read from database or create a new entity and then save it or delete. You will benefit a lot from Eloquent's features such as dirty checking (to send SQL UPDATE only for the fields which have been changed), model events (e.g. to send administrative alert or update statistics counter when someone has created a new account), traits (timestamps, soft deletes, custom traits) eager/lazy loading etc.

But, as you already know, it comes with some performance price. When you process a single or just a few records, there is nothing to worry about. But for cases when you read lots of records (e.g. for datagrids, for reports, for batch processing etc.) the plain DB is better approach. Yes you may said Laravel Query Builder is the best choice in this case.

For our application we are doing exactly that - using Laravel's Eloquent in web forms to process a single record and using DB (with SQL views) to retrieve data for grids, export etc.