Laravel Maximum execution time of 60 seconds exceeded
Solution 1
For some reason, the error didn't appear when I closed my Sqlite DB browser desktop program. Yet this is very strange, since I had the sqlite db browser app open since the beginning of my app creation.
Solution 2
your query is seems to be ok but foreach loop i think it's not ok so update code like this
$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
add line in pubilc/index.php
file function
set_time_limit($seconds);
otherwise increase the max_execution_time
in php.ini
and restart server
Regentix
Updated on September 15, 2022Comments
-
Regentix over 1 year
I have a many to many relation in my laravel application. The model
Competition
belongsToManyLocation
and vice versa.Now I am trying to provide a functionality where one can add existing locations to a competition.
$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); }
I need to check wheter the competition already has the location, so I store competition data inside
$competition
. Afterwards, if the location was not found, I attach the location to the competition.The problem is the amount of queries that are running; one for the competition data, one for the location to retrieve its id, when it hasn't been found inside the competition locations, and one to store the data when attaching.
This returns an error: "Maximum execution time of 60 seconds exceeded"
What is the correct way of doing so? To minimize the amount of queries needed.
Thanks in advance
-
Jacob over 5 yearsLocation list only contains 1 location? Some minor improvements are: Competition::findOrFail($request->input('contestId')); $locationNames = $competition->locations()->pluck('name')->toArray();
-
Regentix over 5 yearsYes, Its the value that was selected within a form select.
-
Jacob over 5 yearsDo you know on wich line the code gets a timeout?
-
Regentix over 5 yearsNo, the Laravel error didn't tell me on which line it timed out. I did found a solution tho, I've posted it.
-
-
Regentix over 5 yearsBut is this the best way to retrieve the data? Because if the timeout is due to the amount of queries being executed, it might not be smart to just increase the time limit, right?
-
Jignesh Joisar over 5 years@Regentix i think foreach loop it is not ok so
remove loop
and usemysql query
i updated my answer -
Regentix over 5 yearsThe foreach loop checks if the result of
$competition->locations
has the$request->input('locationList')
value. If I would implement your code, I would attach the location if the location doesn't exist, but thats not what I want to check, because I already know it does.