编程

Laravel 编程技巧——日志与调试

1107 2022-01-10 07:04:07

日志记录参数

你可以使用 Log::info(),或使用更短的 info() 额外参数信息,来了解更多发生的事情

Log::info('User failed to login.', ['id' => $user->id]);

更方便的 DD

你可以在你的 Eloquent 句子或者任何集合结尾添加 ->dd(),而不是使用 dd($result)

// Instead of
$users = User::where('name', 'Taylor')->get();
dd($users);
// Do this
$users = User::where('name', 'Taylor')->get()->dd();

使用 context 日志

在最新的 Laravel 8.49 中:Log::withContext() 将帮助您区分不同请求之间的日志消息。 如果你创建了中间件并且设置了 context,所有的长消息将包含在 context 中,你将会搜索更容易。

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

快速输出Query的sql

如果你想快速输出一个 Eloquent query的sql 你可以调用 toSql()方法如下:

$invoices = Invoice::where('client', 'James pay')->toSql();

dd($invoices)
// select * from `invoices` where `client` = ?