1

I using Laravel 5.3 .this my query and I want in result array the 'children_rec' renamed to 'node'.

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

UPDATE: Relation code:

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

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

thanks

aimme
  • 6,385
  • 7
  • 48
  • 65
Rohullah Rajaee Rad
  • 571
  • 2
  • 9
  • 33

2 Answers2

0

I don't think Laravel supports aliasing a relationship.

You should rename your relation to your desired name:

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

Then you can call it like:

$boxes = Boxes::with('node')
               ->whereNull('box_id')
               ->with('position')
               ->get()
               ->toJson(128);
Camilo
  • 6,504
  • 4
  • 39
  • 60
0

Using attribute accessors to hide the relationship and then show the accessor in the results, for example:

public function getNodeAttribute() {
  return $this->children_rec;
}
protected $hidden = array('children_rec');
protected $appends = array('node');
Ramses
  • 66
  • 1
  • 6