How Do I Get the Query Builder to Output Its Raw SQL Query as a String?

Given the following code:

DB::table('admin')->get();

I want to generate the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM admin. What Should i do?

Default Asked on June 18, 2020 in Programming.
Add Comment
1 Answer(s)

To output to the screen the last queries run you can utilize this:

DB::enableQueryLog(); // Enable query log

// Your Eloquent query executed by using get()

dd(DB::getQueryLog()); // Show results of log



I believe the most recent queries will be at the bottom of the array.

The Output Will be Look Like This


array:1 [▼
0 => array:3 [▼
    "query" => "select * from `users`"
    "bindings" => []
    "time" => 414.66
  ]
]

Default Answered on June 21, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.