2017-10-09 48 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);