Hello guys,
Sometimes, you may want to execute Laravel query builder into MySQL command line or phpMyAdmin SQL tab. You can't run Laravel query into MySQL command line. You need to convert it to raw SQL query.
In this article, I will show you two ways you can convert Laravel query to raw SQL query.
First method is using enableQueryLog()
and getQueryLog() method. enableQueryLog() method will start loggin query and getQueryLog() method will catch last query.
\DB::enableQueryLog();
$users = \DB::table('users')
->where('id', '1')
->first();
dd(\DB::getQueryLog());
Or Eloquent query using model.
\DB::enableQueryLog();
$users = User::where('id', '1')
->first();
dd(\DB::getQueryLog());
This will return array:
Second way is using toSql()
method before using get() or first()
method.
$users = \DB::table('users')
->where('id', '1')
->toSql();
dd($users);
Or using Model query.
$users = User::where('id', '1')
->toSql();
dd($users);
This will return raw query string.
select * from `users` where `id` = ?
So, this way you can convert Laravel query to raw SQL query.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
Wifi Networks "Device not ready" error solve in Linux
While working on my Ubuntu system,...Form Validation in React16 with Example
This is a React form validation step by...How to Compare current password with hash password in Laravel
In this article we will share with you h...How To Build Your First React JS Application
React.js is one of the most popular Java...Remove Whitespace Characters from Both Ends of a String
Sometimes you have a situation when you...