Need to inspect the query being used by Eloquent? Here's how to reveal it.
Actually, we'll check out a few ways to do this, since it depends on how you need to see the data. Let's get started!
This method will show the raw SQL with ? placeholders.
$query = User::where('email', 'alex@codecourse.com');
dd($query->toSql());
Here's the output:
select * from `users` where `email` = ?
Notice this doesn't show the data being bound into the placeholders in the query. Let's look at getBindings
next to figure that out.
This will show the actual binding values.
$query = User::where('email', 'alex@codecourse.com');
dd($query->getBindings());
Here's the output:
[
"alex@codecourse.com"
]
So, if the toSql
method is helpful, you can use getBindings
whenever needed to show the actual values. There's a way to get both though, up next!
This will give you the entire raw SQL.
dd(User::where('email', 'alex@codecourse.com')->toRawSql());
Here's the output:
select * from `users` where `email` = 'alex@codecourse.com'
So toRawSql
pretty much combined what we've already seen into one helpful dump of the SQL query being used behind-the-scenes.
Whatever method you use, you now have several options to dump the underlying SQL query from Eloquent in Laravel.