Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to run raw query in Laravel

How to run raw query laravel eloquent ?

Aman Jain, February 12, 2022March 28, 2023

Sometimes in laravel we wanted to run raw query like select, insert, delete, alter etc. Laravel have multiple ways to run a raw query using select, prepare or statement method in db builder. In this article i will show you to run the raw query in laravel.

To understand this we will take simple examples of multiple solutions. So here is the list to execute the raw query using DB facade in laravel

  1. Select
  2. Insert
  3. Update
  4. Delete
  5. Statement
  6. unprepared
  7. raw
  8. selectRaw
  9. whereRaw
  10. orderByRaw
  11. groupByRaw
  12. havingRaw

As you can we have multiple ways to create raw query and use of it in different cases.

Select() method of DB class

Select method is used to execute select statement in db builder as below

 
public function index()
{
    $users = \DB::select('SELECT * from users');
    dd($user);
}

Output of above code:

array:1 [▼
  0 => {#1268 ▼
    +"id": 1
    +"name": "Aman"
    +"email": "Aman@yopmail.com"
    +"email_verified_at": null
    +"password": "2121212"
    +"remember_token": null
    +"created_at": null
    +"updated_at": null
  }
]

Passing user inputs to select statement

 
public function index(Request $request)
{
    $users = \DB::select('SELECT * from users where email= ? ',[$request->email]);
    dd($user);
}

Insert() method of DB class

Select method is used to execute select statement in db builder as below

 
public function index(Request $request)
{
    $users = \DB::insert('INSERT INTO `users` (`name`, `email`) VALUES (?, ?)',[$request->email,$request->name]);
   
}

Update() method of DB class

Select method is used to execute select statement in db builder as below

 
public function index(Request $request)
{
    $users = \DB::insert('Update `users` set name =  ? where email = ?',[ $request->name,$request->email]);  
}

delete() method of DB class

Select method is used to execute select statement in db builder as below

 
public function index(Request $request)
{
    $users = \DB::delete('DELETE from `users` where  email = ?',[$request->email]);  
}

Statement() method of DB class

Statement method is used to run alter, drop, create or we can say which doesn’t return any values

 
public function index(Request $request)
{
    $users = \DB::statement('ALTER TABLE `users` CHANGE `name` `full_name` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ');  
}

Unprepared() method of DB class

Unprepared is almost same as Statement method but it does not check of SQL injection and we can run any type of query which doesn’t return any data as below

 
public function index(Request $request)
{
    $users = \DB::unprepared('ALTER TABLE `users` CHANGE `name` `full_name` VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ');  
}

raw() method of DB class

Raw method is used to execute raw query in our in build laravel eloquent methods like where, select, order by as below

 
public function index(Request $request)
{
    $users = \DB::table('users')->where(\DB::raw("email like 'a'"));
    //or
    $users = \DB::table('users')->orderBy(\DB::raw("email")); 
}

selectRaw() method of DB class

selectRaw method is used to execute queries in select statement

 
public function index(Request $request)
{
    $users = \DB::table('users')->select("email, concat(first_name, last_name)")->get();
}

whereRaw() method of DB class

whereRaw method is used to execute queries in where clause

 
public function index(Request $request)
{
    $users = \DB::table('users')->whereRaw('email like "%a%"')->get();
}

Also Read : How to use conditional where clause in Laravel 8 eloquent ?

orderByRaw() method of DB class

orderByRaw method is used to execute queries in orderBy clause

 
public function index(Request $request)
{
    $users = \DB::table('users')->orderByRaw('if(status=1,email,id)')->get();
}

groupByRaw() method of DB class

groupByRaw method is used to execute queries in groupBy clause

 
public function index(Request $request)
{
    $users = \DB::table('users')->groupByRaw('if(status=1,email,id)')->get();
}

Related

Php Laravel laravelmysqlqueryraw

Post navigation

Previous post
Next post

Related Posts

Php How to Export or Convert Html to Excel or CSV in laravel 8 9

How to Export or Convert Html to Excel or CSV in laravel 8 / 9?

May 6, 2022May 13, 2022

Excel or CSV are used to store large set of data to analyses and for reporting. In this article we will learn to export excel or CSV in laravel. This tutorial is best fit to you if you want to understand the basic of export of database table content with…

Read More
Php How to Copy Folder Recursively One Folder to Another in Laravel

How to Copy Folder Recursively One Folder to Another in Laravel ?

May 26, 2022May 26, 2022

In this article i will help you to copy the folders recursively from one folder to another in laravel .File System is used to perform the file system based operations like copy Folder Recursively. For this purpose laravel uses Illuminate\Filesystem\Filesystem class in laravel. Laravel provides inbuilt library to access the…

Read More
Php Creating controller in laravel

How to create controller in Laravel 8?

November 7, 2021November 5, 2023

In Laravel controller is most important part to create the connection between our business logic to our view. we will learn create controller in laravel 8.Laravel is a MVC pattern and C stands for controller. It’s responsible for to receive the input from user, process them and validate the input…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version