Get a Tree from a Graph (Many-toMany) Relationship and filter it #217
-
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 16 replies
-
Hi @nilskretschmer,
Can you share the result (as compared to the expected one)? |
Beta Was this translation helpful? Give feedback.
-
Are you on Laravel 10? I pushed an experimental If it works for you, we can take a look at the remaining questions/issues. |
Beta Was this translation helpful? Give feedback.
-
Did you remove the |
Beta Was this translation helpful? Give feedback.
-
Can you share some sample data for the new schema? How do you know which components are the root components for a page? |
Beta Was this translation helpful? Give feedback.
-
What query would you use to get the root components with this structure? With the original database structure, you can use $constraint = function ($query) use ($page) {
$ids = $page->components()->pluck('id');
$query->whereIn('id', $ids);
};
$components = Component::subgraph($constraint)->get();
$graph = $components->toGraph(); There's a more elegant approach, but it requires an additional package. If you keep the original database structure, I can document it. |
Beta Was this translation helpful? Give feedback.
-
Never mind, I just saw that the more elegant approach doesn't work in your case. I've released a new version with the |
Beta Was this translation helpful? Give feedback.
-
Don't specify a branch but revert the package version in your |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Try adding a custom recursive query constraint: $pageComponents = Component::withRecursiveQueryConstraint(function (Builder $query) use ($page) {
$query->where('component_page.page_id', $page->id);
}, function () use ($constraint) {
return Component::subgraph($constraint)->get();
}); |
Beta Was this translation helpful? Give feedback.
Try adding a custom recursive query constraint:
https://github.com/staudenmeir/laravel-adjacency-list#graphs-recursive-query-constraints