Laravel雄辩如何通过嵌套关系函数传递变量
我希望将变量与雄辩的嵌套关系传递。 我读了这 laravel Eloquent Pass变量与关系功能但是在这个答案中,它不考虑嵌套的关系。
我已经有了这种情况:
Category::where("company", $company)->whereNull("parent_ecommerce_id")->with(['children']);
我的模型
public function children() {
return $this->hasMany(Category::class,'parent_ecommerce_id','category_ecommerce_id')->with("children");
}
可以通过链接发布的建议进行修改
Category::where("company", $company)->whereNull("parent_ecommerce_id")->with(['children' => function($query) use ($company) {
$query->where("company", $company);
}]);
,并在第一次通话中起作用,但是在嵌套回调中,我对“公司”的过滤器没有更多的过滤器。如何在所有嵌套关系中过滤公司?我认为将变量传递给了“儿童($ var)”,但我没有找到“通过”语句中的解决方案。
I wish to pass a variable to nested relationship with eloquent.
I read this Laravel Eloquent pass variable to with relationship function but in that answer it does not look at a nested relationship.
I've this scenario:
Category::where("company", $company)->whereNull("parent_ecommerce_id")->with(['children']);
My model
public function children() {
return $this->hasMany(Category::class,'parent_ecommerce_id','category_ecommerce_id')->with("children");
}
I can modify my first call with this as suggest in link posted
Category::where("company", $company)->whereNull("parent_ecommerce_id")->with(['children' => function($query) use ($company) {
$query->where("company", $company);
}]);
and it works in first call, but in nested callback, I haven't more the filter with "company". How can I filter the company in all nested relationship? I thinked passing variable to "children($var)", but I don't find solution to pass that in "with" statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确地让你,我相信应该更像这样。
如果您想实现的目标是与公司被过滤的孩子一起获取所有类别。
If I get you correctly, I believe it should be more like this.
If what you are trying to achieve is to fetch all Categories that with children, where the companies have been filtered.
我认为这种方式不正确,如果有必要使用第二个变量过滤器,我们必须使用另一种方法来获取所有类别树,如这里所述, https://www.youtube.com/watch?v=hfxr6d2kh44
I think this way is not right, if there is necessary of a second variable filter, we must use another way to get all category tree as is explained here https://www.youtube.com/watch?v=hFxR6D2kH44