How to Execute MySQL Query in 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');
");
}
}
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Case Sensitive Query Search Example
- Laravel Eloquent take() and skip() Query Example
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent whereBetween() Query Example
- Laravel Eloquent selectRaw() Query Example
- Laravel Eloquent Order By Query Example
- Laravel Eloquent Where Query Examples
- How to Create and Use Query Scope in Laravel Eloquent
- How to use Union Query with Laravel Eloquent?
- Example of unionAll in Query Builder Laravel
- Laravel Join with Subquery in Query Builder Example
- How to add LIKE query in Elasticsearch?
- How to Get Query Log in Laravel Eloquent?