Laravel雄辩如何通过嵌套关系函数传递变量

发布于 2025-01-23 05:35:42 字数 888 浏览 0 评论 0原文

我希望将变量与雄辩的嵌套关系传递。 我读了这 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2025-01-30 05:35:42

如果我正确地让你,我相信应该更像这样。

如果您想实现的目标是与公司被过滤的孩子一起获取所有类别。

Category::where("company", $company)->whereNull("parent_ecommerce_id")->whereHas('children', function($query) use ($company) {
    $query->where("company", $company);
}])->with(['children']);

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.

Category::where("company", $company)->whereNull("parent_ecommerce_id")->whereHas('children', function($query) use ($company) {
    $query->where("company", $company);
}])->with(['children']);
揪着可爱 2025-01-30 05:35:42

我认为这种方式不正确,如果有必要使用第二个变量过滤器,我们必须使用另一种方法来获取所有类别树,如这里所述, 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文