How to use Raw Expressions in Laravel 5

One such cool feature us using raw queries in laravel. These expressions will be injected into the query as strings but this is really helpful when you want to use raw expressions like aggregate functions. The queries can be executed using DB::Raw method.

Let’s create a DB::Raw method in laravel 5

$users = DB::table('users')
      ->select(DB::raw('count(*) as parent_count'),
               DB::raw('sum(amount) as parent_amount'))
      ->groupBy('parent_id')
      ->get();

Leave a Reply

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