【发布时间】:2022-06-29 16:45:04
【问题描述】:
我希望将变量传递给与 eloquent 的嵌套关系。 我读了这个Laravel Eloquent pass variable to with relationship function,但在那个答案中它没有看到嵌套关系。
我有这种情况:
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);
}]);
它在第一次调用时有效,但在嵌套回调中,我没有更多带有“公司”的过滤器。如何过滤所有嵌套关系中的公司?我想将变量传递给“children($var)”,但我没有找到在“with”语句中传递它的解决方案。
【问题讨论】:
标签: php laravel eloquent relationship