⬆️ Go to main menu ⬅️ Previous (Factories) ➡️ Next (API)
- Logging with parameters
- More convenient DD
- Log with context
- Quickly output an Eloquent query in its SQL form
- Log all the database queries during development
You can write Log::info()
, or shorter info()
message with additional parameters, for more context about what happened.
Log::info('User failed to login.', ['id' => $user->id]);
Instead of doing dd($result)
you can put ->dd()
as a method directly at the end of your Eloquent sentence, or any Collection.
// Instead of
$users = User::where('name', 'Taylor')->get();
dd($users);
// Do this
$users = User::where('name', 'Taylor')->get()->dd();
New in Laravel 8.49: Log::withContext()
will help you to differentiate the Log messages between different requests.
If you create a Middleware and set this context, all Log messages will contain that context, and you'll be able to search them easier.
public function handle(Request $request, Closure $next)
{
$requestId = (string) Str::uuid();
Log::withContext(['request-id' => $requestId]);
$response = $next($request);
$response->header('request-id', $requestId);
return $response;
}
If you want to quickly output an Eloquent query in its SQL form, you can invoke the toSql() method onto it like so
$invoices = Invoice::where('client', 'James pay')->toSql();
dd($invoices)
// select * from `invoices` where `client` = ?
Tip given by @devThaer
If you want to log all the database queries during development add this snippet to your AppServiceProvider
public function boot()
{
if (App::environment('local')) {
DB::listen(function ($query) {
logger(Str::replaceArray('?', $query->bindings, $query->sql));
});
}
}
Tip given by @mmartin_joo