Laravel 5 +雄辩的toJson/toArray导致奇怪的分段错误 [英] Laravel 5 + Eloquent toJson/toArray causes Strange Segmentation Faults
问题描述
我不想回答自己的问题,所以也许您可以帮助我找到解决此问题的方法.
I hate to be answering my own question, so maybe you can help me find what fixed this.
我有一些雄辩的模型,它们彼此属于,并且我通过这样的关联来建立它们.都是正常的东西.
I have some eloquent models which belongTo each-other, and I set them up via association like this. It's all normal stuff.
不幸的是,此过程使$ device无法正常工作.在下面,您可以看到各个值都可以访问,但是任何形式的jsonification都会破坏服务器而不会出错.
This process unfortunately causes $device to work erratically. Below you can see individual values are accessible but any form of jsonification destroys the server without error.
$device = $truck->device;
if(is_null($device) || empty($device)) {
$device = new Devices;
}
$device->truck()->associate($truck);
$device->fleet()->associate($fleet);
$device->serial = $device_input['serial'];
$device->save();
$truck->device()->associate($device);
$truck->save();
error_log( $device->id ); //OK
error_log( $truck->device->id ); //OK
error_log( $device->toJson() ); //ERROR SEGMENTATION FAULT
error_log( $truck->toArray() ); //ERROR SEGMENTATION FAULT
error_log( $truck->device->toJson() ); //ERROR SEGMENTATION FAULT
error_log( $truck->device->toArray() ); //ERROR SEGMENTATION FAULT
error_log( json_encode($device) ); //ERROR SEGMENTATION FAULT
error_log( json_encode($truck->device) ); //ERROR SEGMENTATION FAULT
推荐答案
Laravel无法正确处理该错误.
Laravel isn't handling the error correctly.
首先,检查您的桌子.当我打印设备表时,我可以看到每次运行此设备时都在创建新设备,这意味着关联显然无法正常工作.
First, check your table. When I printed my devices table I could see that new devices were being created every time I ran this, which means the associations were obviously not working.
我将关联移动到了if语句中,例如:
I moved my associations inside the if statement like:
if(is_null($device) || empty($device)) {
$device = new Devices;
$device->truck()->associate($truck);
$device->fleet()->associate($fleet);
}
因此,仅在创建设备时才会发生关联.您可能会认为将关联设置为已关联的对象不会破坏它.
So that association would only happen when the device is created. You'd think setting the association to the object it's already associated to wouldn't break it.
很奇怪,值可以单独访问,但不能作为json访问.如果有人知道任何laravel开发人员或将其发布为正式错误报告的地方,请告诉我
And it's so weird that values are accessible individually but not as json. If anyone knows any laravel devs or a place to post this as an official bug report let me know
这篇关于Laravel 5 +雄辩的toJson/toArray导致奇怪的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!