2017-10-09 45 views
0

我使用Laravel 5.3。这是我的查询,我想在结果数组中将'children_rec'重命名为'node'。如何在口头上重新命名关系函数?

$boxes = Boxes::with('children_rec') 
       ->whereNull('box_id') 
       ->with('position') 
       ->get() 
       ->toJson(128); 

UPDATE: 关系码:

public function child() 
{ 
    return $this 
     ->hasMany('PTA_OIMS\Boxes', 'box_id'); 
} 

public function children_rec() 
{ 
    return $this->child() 
     ->with('children_rec') 
     ->with('position'); 
} 

感谢

+0

是什么,返回结构是什么样子?为什么你不能操纵数组? – tadman

+0

@tadman:结果结构:https://pastebin.com/NFfWS0s2 –

+0

编辑您的问题以包含任何相关代码,请不要将它作为混乱评论添加。 – tadman

回答

0

我不认为Laravel支持走样的关系。

您应该将有关重命名为所需名称:

public function node() 
{ 
    return $this->child() 
     ->with('node') 
     ->with('position'); 
} 

然后,你可以这样调用它:

$boxes = Boxes::with('node') 
       ->whereNull('box_id') 
       ->with('position') 
       ->get() 
       ->toJson(128);