php - 雄辩的递归渴望关系需要了解范围
问题描述
我有一个从父查询调用的递归关系,如下所示:
$results = SomeModel::with(['children' => function($q) {
$q->enabledOnlyBool(true);
}])
->get();
里面SomeModel
我有关系:
public function children(): HasMany {
return $this->hasMany(__CLASS__, 'parent_id', 'id')
->with('children');
}
在那个关系调用中,有没有办法知道全局范围scopeEnabledOnlyBool($bool)
已应用于父模型?要以递归方式急切加载,了解应用于初始调用的范围将是真正有意义的。
我最接近的方法是找到该方法\Illuminate\Database\Eloquent\Model::hasNamedScope($scope)
,但是它似乎只在范围存在时才返回 true,而不是它是否被应用。
理想情况下,我希望能够执行以下操作:
public function children(): HasMany {
return $this->hasMany(__CLASS__, 'parent_id', 'id')
->with(['children' => function($q) {
if ($whatever) {
// Apply scope
}
}]);
}
解决方案
推荐阅读
- regex - 使用正则表达式提取(重复)包含括号的组
- ruby-on-rails - 对订单中的商品进行分组和计数
- python - 在 Python 中为脚本或交互式会话设置日志记录
- google-app-maker - 如何将 AppMaker 应用程序导出为 zip 文件?
- mongodb - 使用 $lookup “填充”字段而不覆盖现有值
- ignite - Apache Ignite 集群所需的端口
- java - 我无法循环检查数独游戏的数量
- css - 如何将所有样式重置为特定元素的浏览器默认值?
- html - 为什么我的输入表单不会粘在我的按钮上?
- php - 使用公钥进行 PHP RSA 加密的问题不返回任何内容