1

I have a model in laravel(5.3)which represents a migration like so:

$table->integer('request_id', true);
$table->string('type_request', 3);
$table->integer('user_id');
$table->string('mis_ord', 25)->nullable();
$table->string('mis_ord_production', 25)->nullable();
$table->dateTime('timestamp_create')->nullable();
$table->timestamp('timestamp_update')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->dateTime('timestamp_processed')->nullable();
$table->integer('prod_hg_id')->index('fk_requests_prod_hg_idx');
$table->integer('prod_sg_id')->index('fk_requests_prod_sg_idx');
$table->string('descr', 50)->nullable();
$table->integer('quantity_2del');
$table->dateTime('date_time_2del')->nullable();
$table->integer('quantity_del')->nullable();
$table->dateTime('date_time_del')->nullable();
$table->integer('basket_id')->index('fk_request_basket_idx');
$table->string('ref_customer', 100)->nullable();
$table->decimal('price', 12)->nullable()->default(0.00);
$table->decimal('price_calc', 12)->nullable()->default(0.00);
$table->decimal('discount_prc', 12)->nullable()->default(0.00);
$table->decimal('discount_bdr', 12)->nullable()->default(0.00);
$table->integer('env_id')->nullable()->index('fk_xps_envelop_idx');
$table->integer('alternative_from')->nullable();
$table->integer('version')->nullable()->default(0);
$table->integer('version_id')->nullable();
$table->string('remark', 256)->nullable();
$table->integer('art_id')->nullable()->index('fk_xps_request_art_idx');
$table->integer('request_status_id')->nullable();
$table->integer('sys_bu_id')->unsigned()->nullable();
$table->boolean('digital')->default(0);
$table->foreign('sys_bu_id')->references('id')->on('sys_bu');

When I try to json_encode an instance of this model like so:

$xpsRequest = xpsRequest::find($id);
json_encode($xpsRequest);

I get an error in my log: Fatal error: Maximum function nesting level of '256' reached, aborting!

I tried to set the xdebug.max_nesting_level to 1000, but the same error is still triggered, even when I run json_encode($xpsRequest, JSON_NUMERIC_CHECK, 1); I also tried to json_encode a variable which only contains a number, which it has no issues with. I also tried to remove all variables and functions from the model, so it is technically a plain model, but the issue remains.

This issue is only existing on my machines, I've had colleagues who do not have this issue.

I verified that all settings are the same, but that did not resolve my issue. I am currently running on:
-Windows 8.1 Pro.
-Apache 2.4.23
-Php 5.6.25
-Mysql 5.7.14

If anyone has a clue what might be going on, or how I can further troubleshoot the issue, any help is welcome. Thank you in advance!

asked Apr 19, 2017 at 15:47
0

1 Answer 1

1

The laravel object is much more complex than just your data values. It will probably try to resolve all relationships as well and you might get into a recursion loop.

To do this correctly you have to use the laravel serialization methods. So your code will look like this:

$xpsRequest = xpsRequest::find($id);
echo $xpsRequest->toJson();

As taken from the docs here: https://laravel.com/docs/5.3/eloquent-serialization


As that didn't seem to resolve the issue (laravel doesn't seem to limit it to the object and goes through the relations anyways is my guess) you can try with the attributesToArray method.

This should only get you the attributes of your object and not the relationships with it resulting in this code:

$xpsRequest = xpsRequest::find($id);
echo json_encode($xpsRequest->attributesToArray());
answered Apr 19, 2017 at 16:01

3 Comments

Thanks for your response. I will test it out as soon as I have the chance.
I doublechecked with the debugger. The function toJson technically still does a json encode, but via a controlled way( it sums up all attributes and calls toArray for them). When I use the json_encode, it goes trough the same set of code, namely jsonSerialize. The issue is sadly not resolved by this change.
Thank you for going deeper into this. Appearently, the code manipulated the model before it was json encoded, and running json encode BEFORE this modification, resulted in a successful execution. The issue was that we used $model->toManyRelationship()->associate($child), without saving the model( it had to be an offline representation BEFORE you store it to the db). There was a reference back to the starting model in the associated model, which clearly could hang up a recursive function. Thank you for your help! I will +1 your answer since it was indeed helpful.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.