Support the ongoing development of Laravel.io →
Database

i have two tables abc and def. i have models of these two tables named respectively Abc and Def. i Abc Model i declare these two functions.

public function child()
    {
       return $this->hasMany('Abc', 'parent');
    }

    // recursive, loads all descendants
    public function children()
    {
       return $this->child()->with('children')->orderBy('sort_order');
       
    }

    // parent
    public function parent()
    {
       return $this->belongsTo('Abc','parent');
    }

    // all ascendants
    public function parentRecursive()
    {
       return $this->parent()->with('parentRecursive');
    }

in controllers i called these functions by

    $abc = Abc::with('children')->whereNull('parent')->get();

i have implemnted n level hierarchy JSON from table through parent child relation.i have common column name in both tables. through these method i get the title column of abc table.but i want to fetch title column of def table.how we can do this??

Thanks HARSH SHAH

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.