[英]Where clause not filtering
In my model I have the following two functions: 在我的模型中,我具有以下两个功能:
public function Children()
{
return $this->hasMany(Menu::class, 'parent_menu_id', 'id');
}
public function ActiveChildren()
{
$securityLevel = Auth()->User()->security_level_id;
$activeChildren = Menu::Children()
->where('active_', TRUE)
->where('security_level_id', $securityLevel);
return $activeChildren;
}
Children()
returns a list of all Menu items where their parent_menu_id matches the id of this record. Children()
返回所有Menu项的列表,这些项的parent_menu_id与该记录的ID相匹配。 This is used in Nova for setup purposes. 在Nova中用于设置目的。
With ActiveChildren()
I am trying to create a filtered list of items for the actual menu where active_
= TRUE and security_level_id
= security level id of the current user. 使用ActiveChildren()
我试图为实际菜单创建一个过滤的项目列表,其中active_
= TRUE和security_level_id
=当前用户的安全级别ID。
But instead, ActiveChildren()
returns all menu items instead of the filtered set. 但是, ActiveChildren()
返回所有菜单项,而不是过滤后的集合。 ActiveChildren populates an array in the following static function: ActiveChildren使用以下静态函数填充数组:
public static function Tree()
{
return static::with(implode('.', array_fill(0, 4, 'ActiveChildren')))
->where('menu_type', '=', 'PRT')
->get();
}
That is then loaded via the AppServiceProvider whenever the menu is included in a blade file: 然后,只要刀片文件中包含菜单,就会通过AppServiceProvider加载该文件:
public function boot()
{
view()->composer('desktop.menu.parent', function ($view) {
$items = Menu::Tree();
$view->withItems($items);
});
}
All this works fine, just the menu items are not filtered, any ideas? 所有这些工作正常,只是菜单项未过滤,有什么想法吗?
It looks like you need to update your ActiveChildren()
relation: 看来您需要更新ActiveChildren()
关系:
public function ActiveChildren()
{
$securityLevel = Auth()->User()->security_level_id;
/* $activeChildren = Menu::Children()
* ->where('active_', TRUE)
* ->where('security_level_id', $securityLevel);
*
* return $activeChildren;
*/
return $this->Children() // switch Menu::Children() to $this->Children()
->where('active_', TRUE)
->where('security_level_id', $securityLevel);
}
尝试这个:
\App\model_name::where('active_',TRUE)->where('security_level_id', $securityLevel);
After some more investigation, I have found the issue to be in my blade code. 经过更多调查后,我发现问题出在刀片服务器代码中。
I still referenced $item['children']
instead of $item['activechildren']
我仍然引用$item['children']
而不是$item['activechildren']
<li>{{ $item['menu_text'] }}</li>
@if (count($item['activechildren']) > 0)
<ul>
@foreach($item['activechildren'] as $item)
@include('desktop.menu.children', $item)
@endforeach
</ul>
@endif
Why did $item['children']
still work? 为什么$item['children']
仍然有效? Why wasn't this throwing an error? 为什么这没有引发错误?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.