How to Execute MySQL Query in Laravel?

By Hardik Savani November 5, 2023 Category : 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 :
Shares