Support the ongoing development of Laravel.io →
posted 7 years ago
Eloquent
Last updated 6 months ago.
0

vioza,

You need to show more of your code.

Does the class extend baum?

class Categorie extends Baum\Node {

Did you set up a migration for the table(s)?

You need to create the root node (parent) first. http://etrepat.com/baum/#creating-root-node

$root = Category::create(['name' => 'Root category']);

Then you can assign children to that parent: http://etrepat.com/baum/#inserting-nodes

$child1 = $root->children()->create(['name' => 'Child 1']);

Then you can search for the parent like so: http://etrepat.com/baum/#node-chains

$node = Category::where('name', '=', 'Root category')->first();

foreach($node->getDescendantsAndSelf() as $descendant) {
  echo "{$descendant->name}";
}

Taken from: http://etrepat.com/baum/#usage

Last updated 7 years ago.
0

Hi Arikin,

Thanks for your reply.

I have been testing allot with baum in the last days.

Currently i'm busy with the code below. I would like to accomplish the following:

Read parent categories that are attached to a topmenu == 1. Fetch all children with a maxdepth of 1. So basicly a parent / child situation.

The code below fires 5 query's. Is there any way (besides caching the result) without this much query's? Query = 1 for parent, 1 for each child.

"select * from `categories` where `topmenu` = ?" 

string(149) "select * from `categories` where `categories`.`parent_id` = ? and `categories`.`parent_id` is not null and `depth` between ? and ? order by `lft` asc"


string(149) "select * from `categories` where `categories`.`parent_id` = ? and `categories`.`parent_id` is not null and `depth` between ? and ? order by `lft` asc"

string(149) "select * from `categories` where `categories`.`parent_id` = ? and `categories`.`parent_id` is not null and `depth` between ? and ? order by `lft` asc"

string(149) "select * from `categories` where `categories`.`parent_id` = ? and `categories`.`parent_id` is not null and `depth` between ? and ? order by `lft` asc"
$tree = Categorie::where('topmenu', '=', 1)->get();
		
		echo '<pre>';
		print_r($tree);
		

		foreach($tree as $t){
			
			echo $t->name.'<br />';
			
			$children = $t->children()->limitDepth(1)->get();	
			
			foreach($children as $c){
				echo $c->name.'<br />';
			}
		}
0

I found a solution.

I created a service provider / facade.

Created a class that receives the full baum tree. Then it removes unnecessary child elements from the tree and return it.

Is that the way to go?

0

Sign in to participate in this thread!

LaraJobs

Your banner here too?

vioza vioza Joined 18 Feb 2015

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.

© 2022 Laravel.io - All rights reserved.