Blablacar
Blablacar

Reputation: 623

How to format array with relations parent-child?

I have the typical structure of table in MySQL.

id | parent_id | name | object_id 
1    0           G      1
2    1           T      1
3    1           R      1

How to build result array with values of parent/child when I select data by object_id?

Upvotes: 0

Views: 320

Answers (1)

larsemil
larsemil

Reputation: 876

If its laravel and the parent and the object are both Eloquent models you should be able to do something like this:

class Parent{
    public function children(){
        return $this->belongsToMany('App\Object', 'your_table', 'parent_id, 'object_id');
    }

}

And then the child object class:

class object{
       public function parent(){
            return $this->belongsToMany('App\Parent', 'your_table', 'object_id, 'parent_id');
        }

}

Look into eloquent relationships here: Eloquent relations If its any other way you are looking for the comments above surely might help you in the correct direction.

Upvotes: 1

Related Questions