[英]How many times does Laravel 5.7 execute the SQL query?
在Laracasts.com上学习Laravel 5.7,它展示了如何使用Eloquent模型对象从数据库中获取1:N
关系记录,如下所示。
// One SQL query is being executed here.
$project = Project::first();
// Another SQL query must be executed here to be abled to count the tasks. Right?
if ($project->tasks->count()) {
// Is another SQL query being executed here to fetch the task's records related to the project?
foreach ($project->tasks as $task) {
echo $task->name;
}
}
使用上述方法已执行了多少个SQL查询? 我不确定正在执行2或3个SQL查询。
您正在寻找的是DB :: enableQueryLog / DB::getQueryLog
:
Laravel可以选择将所有针对当前请求运行的查询登录到内存中。 请注意,在某些情况下(例如,插入大量行时),这可能导致应用程序使用过多的内存。 要启用日志,可以使用enableQueryLog
method: DB::connection()->enableQueryLog();
\DB::enableQueryLog();
// One SQL query is being executed here.
$project = Project::first();
// Another SQL query must be executed here to be abled to count the tasks. Right?
if ($project->tasks->count()) {
// Is another SQL query being executed here to fetch the task's records related to the project?
foreach ($project->tasks as $task) {
echo $task->name;
}
}
# queries so far
\DB::getQueryLog();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.