
Print query in laravel
Created at 13-Mar-2021 ,
By samar
Print query in laravel
Good day, guys. In this post, we’ll look at how to solve the "Print query in laravel" programming puzzle.
-
--PATH app\Http\Controllers\<YourController.php>
$users = User::where('id', '=', 1)->dd(); // or $users = DB::table('users')->where('id', '=', 1)->dd();
DD method is used to print query in laravel. DD stands for the die and dump which is a built-in function used in laravel. DD method prints query information and then stops executing the script. -
--PATH app\Http\Controllers\<YourController.php>
$users = User::where('id', '=', 1)->toSql(); dd($users);
toSql() method gets the raw sql query. This method is good for quickly seeing the SQL query but It doesn't include the query bindings. You have to use the dd() method after toSql() to print the query in your controller file. -
--PATH app\Http\Controllers\<YourController.php>
$users = User::where('id', '=', 1)->dump(); // or $users = DB::table('users')->where('id', '=', 1)->dump();
Dump() method will print the query information but it does not stop the execution of the script. -
--PATH app\Http\Controllers\<YourController.php>
DB::enableQueryLog(); // Enable query log $employees = Employee::get(); $employeeDetails = EmployeeDetail::get(); dd(DB::getQueryLog()); // Show results of log
DB::enableQueryLog(); and dd(DB::getQueryLog()); are the methods which are used to print queries in laravel. You can enable query log using DB::enableQueryLog(); and after that you can get all queries logs using DB::getQueryLog() method. It will display all queries logs which are used after DB::enableQueryLog(); and before dd(DB::getQueryLog()) method. It also displays query bindings. You have to change the model name as per your requirements. -
--PATH app\Http\Controllers\<YourController.php>
DB::connection()->enableQueryLog(); $employeeDetail = EmployeeDetail::all(); echo '<pre>'; print_r(DB::getQueryLog()); die;
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- Generate unique username in Laravel
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- OrderBy on Eloquent relationships method in Laravel
- On delete set foreign id column value null using migration in laravel 8
- First and last item of the array using foreach iteration in laravel blade
- Remove array keys and values if it does not exist in other array in Laravel
- Extract only time from datetime in laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- If no route matched route::fallback in laravel
- Insert Comma Separated Values in laravel
- Get all users except the followings users in overtrue laravel-follow
- How to pass link from controller to view in laravel on ajax call
- Redirect to previous page or url in laravel
- Convert input array to comma-separated string in laravel controller
- How to check relationship is loaded or not in Laravel
- How to customize or Change validation error messages
- Validation errors for multiple forms on same page Laravel
- If condition in foreach loop in laravel
- Get count of filter data, while return a small set of records
- How to upload image in laravel 8
- Class 'App\Providers\Auth' not found
- How to start websocket server in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Composer\Exception\NoSslException