Skip to content

Latest commit

 

History

History
73 lines (55 loc) · 2.34 KB

Log_and_Debug.md

File metadata and controls

73 lines (55 loc) · 2.34 KB

Log and debug

⬆️ Go to main menu ⬅️ Previous (Factories) ➡️ Next (API)

Logging with parameters

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]);

More convenient DD

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();

Log with context

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;
}

Quickly output an Eloquent query in its SQL form

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

Log all the database queries during development

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