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, but the accepted answer is using wamp as his server and I am using LAMP.

What / where is the mistake that I have made ?

Any help is highly appreciated. Thanks.

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');
    }
}

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

Community
  • 1
  • 1
Saiyan Prince
  • 3,930
  • 4
  • 28
  • 71
  • Probably the client doesn't have xdebug installed. At least I don't recommend it for use in production. – Charlotte Dunois Sep 07 '15 at 07:31
  • I agree to what you are saying, but I am unable to test this as it keeps throwing the fatal error exception ? What should I do ? – Saiyan Prince Sep 07 '15 at 07:34
  • Does the error still persists if you increase max_nesting_level to 500? In that case probably some kind of recursion is going on. Maybe it's happening in ProductRequest class or in your admin_logged_in middleware. – maxwilms Sep 07 '15 at 08:16
  • @maxwilms Kindly have a look at the problem. I don't think it is because of the `admin_logged_in` middleware or because of the `ProductRequest` class since I can create / update / delete the categories without any issue. – Saiyan Prince Sep 07 '15 at 08:34
  • @AD7six should I delete all the codes and replace it with only stack trace ? – Saiyan Prince Sep 07 '15 at 08:38
  • Much better. The stack trace does not appear to indicate a loop - so if increasing the nesting limit fixes the problem the "problem" is the complexity of the application. No production system should have xdebug installed, **none**. So, whilst you should probably see if you can reduce nesting (which depends on your application code aswell as your chosen framework) it _is_ a solution to just up xdebug's limit - if that works - as [php itself does not have that limit](http://stackoverflow.com/a/4293870/761202). – AD7six Sep 07 '15 at 08:55
  • I have made changes in the nesting limit and restarted the web server.. But now, there is another error, the already selected categories are not seen after the changes. – Saiyan Prince Sep 07 '15 at 09:32

1 Answers1

-1

Im sorry for the lack of precisions, but it looks like there is a recursive function that go way deeper, probably because of a database query using the crappy eloquent ORM.

I could suggest you to try to target which function call make this happend, and try to rewrite the SQL statement by yourself..

segfault
  • 95
  • 7
  • If I write the query, then I would not be able to use Form Model Binding, which I don't want. So, I appreciate your solution but this is not what I am looking for.. – Saiyan Prince Sep 07 '15 at 07:42
  • I know you are entitled to your opinion but calling Eloquent ORM crappy is flat out below the belt. – Michael Obi Sep 07 '15 at 10:01
  • you're right, its not crappy but just bothering me about the Huge amount of request it could avoid using join's or stuff like this. But i thanks the team that developped Eloquent, so i can use it with laravel framework. – segfault Sep 08 '15 at 07:24