How to solve a timeout error in Laravel 5

PhpLaravel

Php Problem Overview


I have the following set up:

In routes I have:

Route::get('articles', 'ArticlesController@index');

The index method in the controller is simply:

public function index()
{
   $articles = Article::all();
   return View('articles.index', compact('articles'));
}

and in the view:

@extends('../app')
@section('content')
<h1>Articles</h1>
<p>
    @foreach($articles as $article)
    <article>
        <h2><a href="{{action('ArticlesController@show', [$article->id])}}">{{$article->title}}</a></h2>
        <p>{{ $article->body }}</p>
    </article>
    @endforeach
</p>
@stop

I attempted to replace the:

$articles = Article::all();

with

$article = Article::latest()->get();

such that I can actually show the articles latest first. I got the error:

FatalErrorException in Str.php line 322:
Maximum execution time of 30 seconds exceeded

and the call stack is:

in Str.php line 322
at FatalErrorException->__construct() in HandleExceptions.php line 131
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 116
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at Str::snake() in helpers.php line 561
at snake_case() in ControllerInspector.php line 105
at ControllerInspector->getVerb() in ControllerInspector.php line 78
at ControllerInspector->getMethodData() in ControllerInspector.php line 39
at ControllerInspector->getRoutable() in Router.php line 251
at Router->controller() in Router.php line 226
at Router->controllers() in Facade.php line 210
at Facade::__callStatic() in routes.php line 21
at Route::controllers() in routes.php line 21
in RouteServiceProvider.php line 40

... etc

I have restored the controller method to what it was, but the error persists.

Can you please tell me how I can solve this problem?

Php Solutions


Solution 1 - Php

The Maximum execution time of 30 seconds exceeded error is not related to Laravel but rather your PHP configuration.

Here is how you can fix it. The setting you will need to change is max_execution_time.

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30     ; Maximum execution time of each script, in seconds
max_input_time = 60	; Maximum amount of time each script may spend parsing request data
memory_limit = 8M      ; Maximum amount of memory a script may consume (8MB)

You can change the max_execution_time to 300 seconds like max_execution_time = 300

You can find the path of your PHP configuration file in the output of the phpinfo function in the Loaded Configuration File section.

Solution 2 - Php

it's a pure PHP setting. The alternative is to increase the execution time limit only for specific php scripts, by inserting on top of that php file, the following:

ini_set('max_execution_time', 180); //3 minutes

Solution 3 - Php

In Laravel:

Add set_time_limit(0) line on top of query.

set_time_limit(0);

$users = App\User::all();

It helps you in different large queries but you should need to improve query optimise.

Solution 4 - Php

Sometimes you just need to optimize your code or query, Setting more max_execution_time is not a solution.

It is not suggested to take more than 30 for loading a webpage if a task takes more time need to be a queue.

Solution 5 - Php

set time limit in __construct method or you can set in your index controller also where you want to have large time limit.

public function __construct()
{
    set_time_limit(8000000);
}

Solution 6 - Php

try

ini_set('max_execution_time', $time);
$articles = Article::all();

where $time is in seconds, set it to 0 for no time. make sure to make it 60 back after your function finish

Solution 7 - Php

Add above query

ini_set("memory_limit", "10056M");

Solution 8 - Php

If using PHP7, I would suggest you changing the default value in the public/.htaccess

<IfModule php7_module>
   ...
   php_value max_execution_time 300
   ...
</IfModule>

Solution 9 - Php

Execution is not related to laravel go to the php.ini file In php.ini file set max_execution_time=360 (time may be variable depends on need) if you want to increase execution of a specific page then write ini_set('max_execution_time',360) at top of page

otherwise in htaccess php_value max_execution_time 360

Solution 10 - Php

If you are hosting your Laravel app on Heroku, you can create custom_php.ini in the root of your project and simply add max_execution_time = 60

Solution 11 - Php

I had a similar problem just now. However, this had nothing to do with modifying the php.ini file. It was from a for loop. If you are having nested for loops, make sure you are using the iterator properly. In my case, I was iterating the outer iterator from my inner iterator.

Solution 12 - Php

Options -MultiViews -Indexes

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

#cambiamos el valor para subir archivos
php_value memory_limit 400M
php_value post_max_size 400M
php_value upload_max_filesize 400M
php_value max_execution_time 300 #esta es la linea que necesitas agregar.

Solution 13 - Php

You need to just press CTRL + F5. It will work after that.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeordie GadgieView Question on Stackoverflow
Solution 1 - PhpNoman Ur RehmanView Answer on Stackoverflow
Solution 2 - PhpGrigoreas P.View Answer on Stackoverflow
Solution 3 - PhpUmar TariqView Answer on Stackoverflow
Solution 4 - PhpAlireza AboutalebiView Answer on Stackoverflow
Solution 5 - PhpMahendra PratapView Answer on Stackoverflow
Solution 6 - Phpkarrar kazuyaView Answer on Stackoverflow
Solution 7 - PhpsanjayView Answer on Stackoverflow
Solution 8 - PhpKwaye KantView Answer on Stackoverflow
Solution 9 - Phpshriyash deshmukhView Answer on Stackoverflow
Solution 10 - PhpDonKokoView Answer on Stackoverflow
Solution 11 - PhpNorsebackView Answer on Stackoverflow
Solution 12 - PhpCarlos RamírezView Answer on Stackoverflow
Solution 13 - PhpSandro CagaraView Answer on Stackoverflow