How use Raw MySQL Query in Laravel 8

In this example we learn How use Raw MySQL Query in laravel 7, laravel 8. Also in this post we learn Insert raw query in laravel 7, laravel 8. Select raw query in laravel 7, laravel 8. Edit raw query in laravel 7, laravel 8 and Delete raw query in laravel 7, laravel 8.

use Raw MySQL Query in Laravel 8

Step 1 : Create Model

In this step we create Demo model for use raw mysql query in laravel 8.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Demo extends Model
{
    use HasFactory;
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table = 'demo';
    protected $fillable = ['name','email','username','password'];
}

Insert Raw Query in Laravel 7, Laravel 8

Route::get("insert", function(){
    DB::insert('insert into demo (name, email, username, password) values (?, ?, ?, ?)', ['abc', 'abc@gmail.com','abc','abc']);
});

Select Raw Query in Laravel 7, Laravel 8

Route::get("read", function(){
   return DB::select('select * from demo where id = ?',[1]);
});

Update Raw Query in Laravel 7, Laravel 8

Route::get("update", function(){
    DB::update('update demo set username = ? where id = ?',["dharmik",2]);
});

Delete Raw Query in Laravel 7, Laravel 8

Route::get("delete", function(){
    DB::delete('delete from demo where id = ?',[1]);
});

I hope you understand of Raw Query in laravel…