@markdown# Logging with parametersYou can write `Log::info()`, or shorter `info()` message with additional parameters, for more context about what happened.```phpLog::info('User failed to login.', ['id' => $user->id]);```### More convenient DDInstead of doing `dd($result)` you can put `->dd()` as a method directly at the end of your Eloquent sentence, or any Collection.```php// Instead of$users = User::where('name', 'Taylor')->get();dd($users);// Do this$users = User::where('name', 'Taylor')->get()->dd();```### Log with contextNew 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.```phppublic 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;}```### Quickly output an Eloquent query in its SQL formIf you want to quickly output an Eloquent query in its SQL form, you can invoke the toSql() method onto it like so```php$invoices = Invoice::where('client', 'James pay')->toSql();dd($invoices)// select * from `invoices` where `client` = ? ```Tip given by [@devThaer](https://twitter.com/devThaer/status/1438816135881822210)### Log all the database queries during developmentIf you want to log all the database queries during development add this snippet to your AppServiceProvider```phppublic function boot(){ if (App::environment('local')) { DB::listen(function ($query) { logger(Str::replaceArray('?', $query->bindings, $query->sql)); }); }}```Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1473262634405449730)@endmarkdown
We use cookies to improve your experience and to help us understand how you use our site. By using this site, you accept our use of cookies. Cookie Info x