ItSolutionStuff.com

How to Execute MySQL Query in Laravel?

By Hardik Savani • November 5, 2023
Laravel

Sometimes, we require to run directly mysql query on Laravel for example if you need to copy one table to other table using mysql query. I also need in one project and that's why i am fetching problem. But you can use sql query on laravel using db statement in bellow example you can learn how to run.

Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

class PostController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

DB::statement("

INSERT INTO `posts` (`id`, `title`, `body`, `created_at`, `updated_at`, `slug`) VALUES (NULL, 'Demo Title', 'This is body', '2023-04-26 09:34:51', '2023-04-26 09:34:51', 'demo-body');

");

}

}

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Case Sensitive Query Search Example

Read Now →

Laravel Eloquent take() and skip() Query Example

Read Now →

Laravel Eloquent whereNotNull() Query Example

Read Now →

Laravel Eloquent whereBetween() Query Example

Read Now →

Laravel Eloquent selectRaw() Query Example

Read Now →

Laravel Eloquent Order By Query Example

Read Now →

Laravel Eloquent Where Query Examples

Read Now →

How to Create and Use Query Scope in Laravel Eloquent

Read Now →

How to use Union Query with Laravel Eloquent?

Read Now →

Example of unionAll in Query Builder Laravel

Read Now →

Laravel Join with Subquery in Query Builder Example

Read Now →

How to add LIKE query in Elasticsearch?

Read Now →

How to Get Query Log in Laravel Eloquent?

Read Now →