Geordie Gadgie Geordie Gadgie - 1 year ago 90
PHP Question

How to solve a timeout error in Laravel 5

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:

@foreach($articles as $article)
<h2><a href="{{action('ArticlesController@show', [$article->id])}}">{{$article->title}}</a></h2>
<p>{{ $article->body }}</p>

I attempted to replace the:

$articles = Article::all();


$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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download