MehulBawadia's avatar

Maximum function nesting level of '100' reached, aborting Laravel 5.1

I am making an ecommerce web application in Laravel 5.1. While examining the application's admin panel for editing a particular product, I got an exception:

FatalErrorException in Container.php line 1057:
Maximum function nesting level of '100' reached, aborting!

I have implemented Repository Pattern in my application and it is still in development stage.

Here's the stack trace:

in Container.php line 1057
at FatalErrorException->__construct() in HandleExceptions.php line 133
at HandleExceptions->fatalExceptionFromError() in HandleExceptions.php line 118
at HandleExceptions->handleShutdown() in HandleExceptions.php line 0
at Container->make() in Application.php line 674
at Application->make() in Container.php line 1157
at Container->offsetGet() in DatabaseManager.php line 251
at DatabaseManager->getDefaultConnection() in DatabaseManager.php line 85
at DatabaseManager->parseConnectionName() in DatabaseManager.php line 61
at DatabaseManager->connection() in Model.php line 3209
at Model::resolveConnection() in Model.php line 3175
at Model->getConnection() in Model.php line 2946
at Model->getDateFormat() in Model.php line 2879
at Model->fromDateTime() in Model.php line 2838
at Model->setAttribute() in Model.php line 422
at Model->fill() in Model.php line 445
at Model->Illuminate\Database\Eloquent\{closure}() in Model.php line 2295
at Model::unguarded() in Model.php line 446
at Model->forceFill() in Pivot.php line 54
at Pivot->__construct() in Model.php line 1937
at Model->newPivot() in BelongsToMany.php line 1124
at BelongsToMany->newPivot() in BelongsToMany.php line 1137
at BelongsToMany->newExistingPivot() in BelongsToMany.php line 257
at BelongsToMany->hydratePivotRelation() in BelongsToMany.php line 178
at BelongsToMany->get() in BelongsToMany.php line 99
at BelongsToMany->getResults() in Model.php line 2699
at Model->getRelationshipFromMethod() in Model.php line 2665
at Model->getRelationValue() in Model.php line 2607
at Model->getAttribute() in Model.php line 3325
at Model->__get() in Model.php line 46
at Product->getCategoryIdAttribute() in Model.php line 2722
at Model->mutateAttribute() in Model.php line 2624
at Model->getAttributeValue() in Model.php line 3394
at Model->__isset() in FormBuilder.php line 3348
at Model->offsetExists() in FormBuilder.php line 404
at data_get() in FormBuilder.php line 1048
at FormBuilder->getModelValueAttribute() in FormBuilder.php line 1035
at FormBuilder->getValueAttribute() in FormBuilder.php line 487
at FormBuilder->select() in Facade.php line 219
at Facade::__callStatic() in 400c2ba760310cd2966e41d1b593cf85 line 24
at FormFacade::select() in 400c2ba760310cd2966e41d1b593cf85 line 24
in PhpEngine.php line 42
at PhpEngine->evaluatePath() in CompilerEngine.php line 58
at CompilerEngine->get() in View.php line 138
at View->getContents() in View.php line 107
at View->renderContents() in View.php line 81
at View->render() in e6c9a42799c231accc0d1cb4aa2d16d0 line 47
in PhpEngine.php line 42
at PhpEngine->evaluatePath() in CompilerEngine.php line 58
at CompilerEngine->get() in View.php line 138
at View->getContents() in View.php line 107
at View->renderContents() in View.php line 81
at View->render() in Response.php line 51
at Response->setContent() in Response.php line 202
at Response->__construct() in Router.php line 1225
at Router->prepareResponse() in ControllerDispatcher.php line 113
at ControllerDispatcher->Illuminate\Routing\{closure}() in Pipeline.php line 139
at call_user_func() in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}() in AdminLoggedIn.php line 20
at AdminLoggedIn->handle() in Pipeline.php line 124
at call_user_func_array() in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}() in Pipeline.php line 103
at call_user_func() in Pipeline.php line 103
at Pipeline->then() in ControllerDispatcher.php line 114
at ControllerDispatcher->callWithinStack() in ControllerDispatcher.php line 69
at ControllerDispatcher->dispatch() in Route.php line 201
at Route->runWithCustomDispatcher() in Route.php line 134
at Route->run() in Router.php line 704
at Router->Illuminate\Routing\{closure}() in Pipeline.php line 139
at call_user_func() in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}() in Pipeline.php line 103
at call_user_func() in Pipeline.php line 103
at Pipeline->then() in Router.php line 706
at Router->runRouteWithinStack() in Router.php line 671
at Router->dispatchToRoute() in Router.php line 631
at Router->dispatch() in Kernel.php line 236
at Kernel->Illuminate\Foundation\Http\{closure}() in Pipeline.php line 139
at call_user_func() in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}() in VerifyCsrfToken.php line 50
at VerifyCsrfToken->handle() in Pipeline.php line 124
at call_user_func_array() in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}() in ShareErrorsFromSession.php line 54
at ShareErrorsFromSession->handle() in Pipeline.php line 124
at call_user_func_array() in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}() in StartSession.php line 62
at StartSession->handle() in Pipeline.php line 124
at call_user_func_array() in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}() in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle() in Pipeline.php line 124
at call_user_func_array() in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}() in EncryptCookies.php line 59
at EncryptCookies->handle() in Pipeline.php line 124
at call_user_func_array() in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}() in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle() in Pipeline.php line 124
at call_user_func_array() in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}() in Pipeline.php line 103
at call_user_func() in Pipeline.php line 103
at Pipeline->then() in Kernel.php line 122
at Kernel->sendRequestThroughRouter() in Kernel.php line 87
at Kernel->handle() in index.php line 54
in server.php line 21
at {main}() in server.php line 0

The problem is when I try to edit the product from the admin panel, I get the above exception, though I can insert a new product from the admin panel.

I know that I can increase the xdebug.max_nesting_level = 100 to xdebug.max_nesting_level = 500, but this is not what I am looking for. I am developing the application for a client and they, probably, are having their own server. Thus, I cannot use this as my solution.

By doing some research, I was brought to this question: http://stackoverflow.com/questions/8656089/solution-for-fatal-error-maximum-function-nesting-level-of-100-reached-abor, but the accepted answer is using wamp as his server and I am using LAMP.

UPDATE 1: After bit of study of the stack trace, I found that, it is with the form that I am using:

form.blade.php

<div class="row">
    <div class="col-md-4 col-sm-6">
        <div class="form-group">
            {!! Form::label('code', 'Code:') !!}
            {!! Form::text('code', null, ['class' => 'form-control input-sm']) !!}
        </div>
    </div>

    <div class="col-md-4 col-sm-6">
        <div class="form-group">
            {!! Form::label('name', 'Name:') !!}
            {!! Form::text('name', null, ['class' => 'form-control input-sm']) !!}
        </div>
    </div>

    <div class="col-md-4 col-sm-6">
        <div class="form-group">
            {!! Form::label('category_id', 'Category:') !!}
            <!-- Here's the error: $categoryList -->
            {!! Form::select('category_id[]', $categoryList, null, ['class' => 'form-control input-sm categoryList', 'multiple']) !!}
        </div>
    </div>
</div>


<div class="row">
    <div class="col-md-4 col-sm-6">
        <div class="form-group">
            {!! Form::label('sort', 'Sort:') !!}
            {!! Form::text('sort', null, ['class' => 'form-control input-sm']) !!}
        </div>
    </div>

    <div class="col-md-12 col-sm-12">
        <div class="form-group">
            {!! Form::label('special_note', 'Special Note:') !!}
            {!! Form::textarea('special_note', null, ['class' => 'form-control input-sm ckeditor', 'id' => 'prdSpecialNote'] ) !!}
        </div>
    </div>
</div>

<div class="form-group">
    {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary btn-block', 'id' => $submitButtonId]) !!}
</div>

Product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;

    /**
     * The dates that will be used as Carbon instance
     * 
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'code', 'name', 'description', 'special_note', 'sort', 'display'
    ];

    /**
     * The Many-To-Many relationship between the category and
     * the product.
     * 
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function categories()
    {
        return $this->belongsToMany('App\Category')->withTimestamps();
    }

    /**
     * Get the array of App\Category.
     * 
     * @return array
     */
    public function getCategoryIdAttribute()
    {
        // If I comment this line, the error disappears
        // but the category_id field becomes blank
        // And if I leave it as it is
        // It throws the error exception

        return $this->categories->lists('id');
    }
}

What / where is the mistake that I have made ?

Any help is highly appreciated. Thanks.

P.S.: I am using Ubuntu 14.04 LTS x64bit Architecture with LAMP installed. Also I am using Form Model Binding.

0 likes
9 replies
bobbybouwmann's avatar

This mostly happens when you have xdebug installed. That will add some calls to your code and therefore it will override the max nesting level. This is something that happens more often and the only solution right now is to higher up your nesting level. Change the setting xdebug.max_nesting_level in your php.ini file to 250 or 500.

Source: http://xdebug.org/docs/all_settings#max_nesting_level

MehulBawadia's avatar

@bobbybouwmann I am failing to update the php.ini file. The loaded configuration file is /etc/php5/apache2/php.ini.. So as you said, I added the **xdebug.max_nesting_level=500** in the last line and restarted the apache server with the following command in the terminal:

sudo service apache2 restart

It did restarted, but when checked the phpinfo() in the browser, it still showed 100 only. What could be the issue ?

NomaD's avatar

Hello,

solution 1 :

instead use

    public function getCategoryIdAttribute()
    {
        return $this->categories()->lists('id');
    }

solution 2 :

dont specify getCategoryIdAttribute inside model just render category list inline in the form

  {!! Form::select('category_id[]',  \App\User::find( $user->id)->categories()->lists('id'), NULL,['class' => 'form-control input-sm categoryList', 'multiple']) !!}

solution 3 :

change xdebug.max_nesting_level highter as needed

MehulBawadia's avatar

@NomaD why you are suggesting me to use option 2 of your own solution ? May I know the reason ??

NomaD's avatar

@IamCrazyD none of above mentioned solutions are approved and suggested globally i am just saying i had same problem and all above solutions stopped error message to appear. in my opinion changing xdebug.max_nesting_level is bad approach, also 2 solution is bit ugly , i found first solution less complicated and better. anyways there is no exact and concrete answer about this problem some says this is caused by form builder, but who knows many of google articles recommend to increase change xdebug.max_nesting_level to 500 reason is that modern frameworks require max_nesting_level to be set higher then default.

MehulBawadia's avatar

@No maD Don't you think that I'll have to add that code again and again to every blade file whenever required ? What I did is I replace the **xdebug.max_nesting_level=100** with **xdebug.max_nesting_level=500**, and the error exception was gone.

NomaD's avatar

@IamCrazyD currently i solved my problem using solution 1 , but if it still appears i will set xdebug.max_nesting_level=500 too. it seems there is no other optimal solution for this.

Please or to participate in this conversation.