
Laravel MySQL Raw query example
Created at 22-Aug-2022 ,
By samar
Laravel MySQL Raw query example
With this article, we will examine several different instances of how to solve the "Laravel MySQL Raw query example".
Raw MySQL query are helpful when you need to write our own SQL queries. Laravel query builder has the tools we need to safely run such queries. Sometimes we have to run the Raw MySQL queries to perform some action on database then raw sql queries are very useful.-
Raw MySQL query Example Code
1. Raw MySQL query with selectRaw method
$users = DB::table('users') ->selectRaw('count(*) as user_count') ->get();
2. Raw MySQL query with DB::raw and DB::select
$results = DB::select( DB::raw("SELECT * FROM users WHERE id = 1"));
3. Raw MySQL query with parameter binding
$id = Input::get("id"); $results = DB::select( DB::raw("SELECT * FROM users WHERE id = :id"), array( 'id' => $id, ));
Import DB facade
use Illuminate\Support\Facades\DB;
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to restore deleted records in laravel
- How to get selected categories on edit record with Select2
- How to create pivot table in laravel using migration
- Call to undefined method Illuminate\Support\Facades\Request::all()
- Cast Array to an Object in Controller and then pass to view in laravel
- How to pass data to multiple partial view files in laravel
- Add class to body in laravel view
- Recursive function example code PHP Laravel
- How to validate form input data in laravel
- Pass value from controller to model in laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Create model with migration and seeder
- Drop foreign key column in Laravel using migration
- Update existing pivot table data in laravel
- Ajax POST request in laravel
- Laravel 9 route group with controller
- How to add class to tr in table using foreach in laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- Create project table with model and migration
- If condition in foreach loop in laravel
- If condition in Laravel 9
- How to pass link from controller to view in laravel on ajax call
- How to check record exist or not in relationship table
- How to add columns in existing table using migration in laravel
- Call to a member function update() on null