Laravel Eloquent selectRaw Query Tutorial

Laravel Eloquent selectRaw Query Tutorial

Laravel selectRaw() query example; In this tutorial, you will learn in detail how to write select raw and select DB raw query in laravel. And as well as learn, how to use selectRaw with joined table data.

You can use the Laravel selectRaw eloquent method to building queries in laravel apps. And also use laravel selectraw with multiple conditions in eloquent queries.

So, let’s see the following examples that will help you on how to use selectRaw() eloquent query in laravel:

  1. Example 1: Laravel selectRaw Query using Model
  2. Example 2: selectRaw Query using Query Builder
  3. Example 3: Laravel selectRaw with joined table data
  4. Example 4: Using Parameters

Example 1: Laravel selectRaw Query using Model

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::select("*")
                        ->selectRaw('amount + ? as amount_with_bonus', [500])
                        ->get();
        dd($users);
    }
}

When you dump the above given selectRaw query you will get the following SQL query:

select *, amount + ? as amount_with_bonus from `users`

Example 2: selectRaw Query using Query Builder

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
use DB;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = DB::table('users')->select("*")
                        ->select('*', DB::raw('amount + 500 as amount_with_bonus'))
                        ->get();
  
        dd($users);
    }
}

When you dump the above given selectRaw query you will get the following SQL query:

select *, amount + ? as amount_with_bonus from `users`

Example 3: Laravel selectRaw with joined table data

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\User;
use DB;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
                 $users = DB::table('users')
                  ->where('active','true')
                  ->join('user_types', 'users.user_type_id', '=', 'user_types.id')
                  ->groupBy('user_type_id','user_types.name')
                  ->selectRaw('sum(total) as sum, user_types.name as name')
                  ->pluck('sum','name');
  
        dd($users);
    }
}

Example 4: Using Parameters

In this example, parameters are used in the raw WHERE clause. The resulting query would be something like: SELECT name, price FROM products WHERE price BETWEEN 100 AND 500.

$minPrice = 100;
$maxPrice = 500;

$products = DB::table('products')
            ->selectRaw('name, price')
            ->whereRaw('price BETWEEN ? AND ?', [$minPrice, $maxPrice])
            ->get();

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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