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!
1 Answer 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());