
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 get route name on visit URL in laravel
- Retain selected value of select box in Laravel
- Method Illuminate\Database\Eloquent\Collection::lists does not exist
- How to set column as primary key in Laravel model
- Get today records in Laravel
- Get posts belongs to a specific user in Laravel
- How to run a specific seeder class in laravel
- Database transactions in laravel
- Get last record from table in laravel
- How to send ID to another page in Laravel
- How to add a key value pair to existing array in laravel
- How to pass data to partial view file in laravel
- How to validate form input data in laravel
- OrderBy on Eloquent relationships method in Laravel
- If condition in Laravel 9
- Seed database using SQL file in Laravel
- How to Access Array in blade laravel
- How to create belongstomany relation using custom name on custom pivot table
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Order by multiple columns in Laravel
- Import/Use Storage facade in laravel
- How to customize pagination view in laravel
- Laravel insert query not working
- Class 'App\Providers\Auth' not found
- How to pass variable from controller to model in Laravel