
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
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- How to automatically update the timestamp of parent model in Laravel
- Array to string conversion laravel blade
- How to get date from created_at field in laravel
- Multiple Level eager loading in Laravel
- Display success message in laravel
- Trying to access array offset on value of type null error in laravel
- Credit card validation in laravel
- Remove array keys and values if it does not exist in other array in Laravel
- How to get single column value in laravel
- How to get selected categories on edit record with Select2
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Order by multiple columns in Laravel
- The use statement with non-compound name 'Auth' has no effect
- Convert multidimensional array to single array in Laravel
- How to upload multiple images after preview in laravel using cropper js
- Update existing pivot table data in laravel
- Laravel delete all rows older than 30 days
- Get Array of IDs from Eloquent Collection
- Post model with title and body in laravel 8
- Target class [HomeController] does not exist
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Get id of last inserted record in laravel
- Get last year created records in Laravel
- How to authenticate admin users in Laravel ?