-1

Desired Output is like :

Parent Cat 1
Parent Cat 2
    Child Cat 1
    Child Cat 2
Parent Cat 3
    Child Cat 3

but i need to do like this while the table structure is like below

   id  | cat_name      | cat_parent_id
   --- | --------------| ------------- 
   1   | Parent Cat 1  | NULL 
   2   | Parent Cat 2  | NULL 
   3   | Child Cat 1   | 2 
   4   | Child Cat 2   | 2 
   5   | Parent Cat 3  | NULL 
   6   | Child Cat 3   | 5

Table structure was like this

I would like to get the output like below in blade view:

Laravel view in table for all parents:

Parent Cat 1
Parent Cat 2
Parent Cat 3

Laravel view in table for all childs:

Child Cat 1
Child Cat 2
Child Cat 3

How get Like this on an above strucutre based on all the parents

Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • You can do an inner join on your table to fetch required data to start with – us190190 Nov 25 '19 at 12:03
  • both parent and child are in a single table and im using eloquent orm in laravel to fetch laravel tree – praveenkumar N Nov 25 '19 at 12:05
  • you want to fetch them separately or in a single eloquent query? Cause the structure and example is not descriptive enough. – us190190 Nov 25 '19 at 12:07
  • i was using the below code for orm public function childs() { return $this->hasMany('App\Model\clients','under_reference','reference_id','id')->leftjoin('clients_payment_type', function($join) { $join->on('clients_payment_type.user_id', '=', 'clients.id'); }); } public function GrandChild() { return $this->hasMany('App\Model\clients','under_reference','reference_id','id')->with("childs")->leftjoin('clients_payment_type', function($join) { $join->on('clients_payment_type.user_id', '=', 'clients.id'); }); } – praveenkumar N Nov 25 '19 at 12:13
  • https://i.stack.imgur.com/mR869.png image for database structure – praveenkumar N Nov 25 '19 at 12:16
  • @praveenkumarN did you get your answer ? – N69S Nov 26 '19 at 13:03
  • @N69S No it was not working – praveenkumar N Nov 27 '19 at 05:59
  • https://stackoverflow.com/questions/59046609/how-do-i-get-all-children-that-fall-under-a-multiple-parent-id-using-eloquent-fo this was my code – praveenkumar N Nov 27 '19 at 06:02
  • @praveenkumarN have you tried my answer (with relation) it works perfectly. – N69S Nov 27 '19 at 08:04
  • @N69S https://i.stack.imgur.com/A8IIQ.png with this image you can find the answer but it executes only the level ones 1st id "IBCN056" values only in level two no other data is getting executed other id from level one to be executed to level two in a single table not to multiple table and then by using the level two id everything to be come to level three – praveenkumar N Nov 27 '19 at 11:48

2 Answers2

0

If you want to fetch both separately then:

$parentCats = DB::table('table_name')->whereNull("cat_parent_id")->pluck("cat_name");

$childCats = DB::table('table_name')->whereNotNull("cat_parent_id")->pluck("cat_name");

Then inside view file, you can iterate over these arrays

us190190
  • 116
  • 8
  • Tree structure is based on the single parent with three levels where the level one contains the parent id as reference and level 2 should get all the child from all the parent of level one same as level 3 but what am i getting is individual parent and individual child's separately but need all the child's in a single table in view public function childs() { return $this->hasMany('App\Model\clients','under_reference','reference_id','id')->leftjoin('clients_payment_type', function($join) { $join->on('clients_payment_type.user_id', '=', 'clients.id'); });} – praveenkumar N Nov 25 '19 at 12:34
0
class Category extends Model
{
//relations
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function parent()
    {
        return $this->belongsTo(self::class, 'cat_parent_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(self::class, 'cat_parent_id');
    }

    public function scopeParentsOnly($query)
    {
        return $query->whereNull('cat_parent_id');
    }
}

Now you can call it like this

$categories = Category::query()->parentsOnly()->with('children')->get();

You will get only the parent categories with the attribute children containing each category children categories.

N69S
  • 16,110
  • 3
  • 22
  • 36