Laravel 超过 60 秒的最大执行时间

Laravel Maximum execution time of 60 seconds exceeded

我的 laravel 应用程序中有多对多关系。 模型 Competition belongsToMany Location 反之亦然。

现在我正在尝试提供一种功能,可以将现有位置添加到比赛中。

$competition    = Competition::where('id','=', $request->input('contestId'))->firstOrFail();
$locations      = $competition->locations;
$locationNames  = [];

foreach ($locations as $location) {
    $locationNames[] = $location->name;
}

if (!in_array($request->input('locationList'), $locationNames)) {
    $locationId = Location::where('name','=', $request->input('locationList'))->firstOrFail()->id;
    $competition->locations()->attach($locationId);
}

我需要检查比赛是否已经有位置,所以我将比赛数据存储在$competition中。之后,如果找不到位置,我将位置附加到比赛中。

问题是 运行 的查询数量;一种用于比赛数据,一种用于在比赛地点内未找到时检索其 ID 的位置,另一种用于在附加时存储数据。

这returns一个错误:"Maximum execution time of 60 seconds exceeded"

这样做的正确方法是什么?尽量减少所需的查询量。

提前致谢

你的查询似乎没问题,但我认为 foreach 循环不对,所以像这样更新代码

$competition = Competition::where('id','=', $request->input('contestId'))->firstOrFail();

if($locationId = Location::where('name','=', $request->input('locationList'))->first()){
      $competition->locations()->attach($locationId->id);
}

if location is there then added otherwise not added

pubilc/index.php 文件中添加行 function

set_time_limit($seconds);

否则在php.ini中增加max_execution_time并重启服务器

出于某种原因,当我关闭我的 Sqlite DB 浏览器桌面程序时,错误没有出现。然而这很奇怪,因为从我的应用程序创建开始我就打开了 sqlite db 浏览器应用程序。

只需清除所有缓存

步骤:1 转到您的项目目录并打开命令提示符

步骤:2 在命令提示符中写入命令 php artisan optimize:clear

然后确认你的问题已经解决了...