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. This is used in Nova for setup purposes.
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.
But instead, ActiveChildren()
returns all menu items instead of the filtered set. ActiveChildren populates an array in the following static function:
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:
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?
php laravel laravel-5
add a comment |
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. This is used in Nova for setup purposes.
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.
But instead, ActiveChildren()
returns all menu items instead of the filtered set. ActiveChildren populates an array in the following static function:
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:
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?
php laravel laravel-5
add a comment |
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. This is used in Nova for setup purposes.
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.
But instead, ActiveChildren()
returns all menu items instead of the filtered set. ActiveChildren populates an array in the following static function:
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:
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?
php laravel laravel-5
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. This is used in Nova for setup purposes.
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.
But instead, ActiveChildren()
returns all menu items instead of the filtered set. ActiveChildren populates an array in the following static function:
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:
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?
php laravel laravel-5
php laravel laravel-5
edited Nov 21 '18 at 7:34
barbsan
2,43631223
2,43631223
asked Nov 20 '18 at 22:02
C. GillC. Gill
238
238
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
It looks like you need to update your ActiveChildren()
relation:
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);
}
Can you clarify what you changed, and why it made a difference? This will make the answer more valuable to both OP and future visitors. (BTW using theauth()
helper is is a perfectly valid way to get the auth object without having to declareuse /Illuminate/Support/Facades/Auth
or whatever. But your change to the second line was definitely needed.)
– miken32
Nov 21 '18 at 2:29
1
@miken32 Thanks for the tip on the auth() helper ... it was the upper case function/method names that were looking wrong to me, but apparently that is valid in PHP.
– Peter
Nov 21 '18 at 2:42
Agreed. It took me a while to see the change here.
– adam
Nov 21 '18 at 2:43
@Peter, thanks for the help, this solved my issue. I have also corrected my usage of upper case in function names. Still learning laravel/php
– C. Gill
Nov 21 '18 at 13:22
1
@C.Gill Laravel Models have some getters that will load the relation if it exists on the Model, even if it isn't loaded yet. So if doesn't behave like an array where you would get an undefined index error when accessing a key that hadn't been set ... even though you are using array style accessors like $model['property'] to get the data.
– Peter
Nov 21 '18 at 18:44
|
show 3 more comments
try this:
Appmodel_name::where('active_',TRUE)->where('security_level_id', $securityLevel);
As this looks nearly the same as the query given inActiveChildren
, can you explain why this one might solve the problem? What was wrong with the original query?
– Nico Haase
Nov 21 '18 at 7:31
add a comment |
After some more investigation, I have found the issue to be in my blade code.
I still referenced $item['children']
instead of $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? Why wasn't this throwing an error?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402258%2fwhere-clause-not-filtering%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you need to update your ActiveChildren()
relation:
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);
}
Can you clarify what you changed, and why it made a difference? This will make the answer more valuable to both OP and future visitors. (BTW using theauth()
helper is is a perfectly valid way to get the auth object without having to declareuse /Illuminate/Support/Facades/Auth
or whatever. But your change to the second line was definitely needed.)
– miken32
Nov 21 '18 at 2:29
1
@miken32 Thanks for the tip on the auth() helper ... it was the upper case function/method names that were looking wrong to me, but apparently that is valid in PHP.
– Peter
Nov 21 '18 at 2:42
Agreed. It took me a while to see the change here.
– adam
Nov 21 '18 at 2:43
@Peter, thanks for the help, this solved my issue. I have also corrected my usage of upper case in function names. Still learning laravel/php
– C. Gill
Nov 21 '18 at 13:22
1
@C.Gill Laravel Models have some getters that will load the relation if it exists on the Model, even if it isn't loaded yet. So if doesn't behave like an array where you would get an undefined index error when accessing a key that hadn't been set ... even though you are using array style accessors like $model['property'] to get the data.
– Peter
Nov 21 '18 at 18:44
|
show 3 more comments
It looks like you need to update your ActiveChildren()
relation:
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);
}
Can you clarify what you changed, and why it made a difference? This will make the answer more valuable to both OP and future visitors. (BTW using theauth()
helper is is a perfectly valid way to get the auth object without having to declareuse /Illuminate/Support/Facades/Auth
or whatever. But your change to the second line was definitely needed.)
– miken32
Nov 21 '18 at 2:29
1
@miken32 Thanks for the tip on the auth() helper ... it was the upper case function/method names that were looking wrong to me, but apparently that is valid in PHP.
– Peter
Nov 21 '18 at 2:42
Agreed. It took me a while to see the change here.
– adam
Nov 21 '18 at 2:43
@Peter, thanks for the help, this solved my issue. I have also corrected my usage of upper case in function names. Still learning laravel/php
– C. Gill
Nov 21 '18 at 13:22
1
@C.Gill Laravel Models have some getters that will load the relation if it exists on the Model, even if it isn't loaded yet. So if doesn't behave like an array where you would get an undefined index error when accessing a key that hadn't been set ... even though you are using array style accessors like $model['property'] to get the data.
– Peter
Nov 21 '18 at 18:44
|
show 3 more comments
It looks like you need to update your ActiveChildren()
relation:
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);
}
It looks like you need to update your ActiveChildren()
relation:
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);
}
edited Nov 21 '18 at 2:48
answered Nov 21 '18 at 2:18
PeterPeter
8841213
8841213
Can you clarify what you changed, and why it made a difference? This will make the answer more valuable to both OP and future visitors. (BTW using theauth()
helper is is a perfectly valid way to get the auth object without having to declareuse /Illuminate/Support/Facades/Auth
or whatever. But your change to the second line was definitely needed.)
– miken32
Nov 21 '18 at 2:29
1
@miken32 Thanks for the tip on the auth() helper ... it was the upper case function/method names that were looking wrong to me, but apparently that is valid in PHP.
– Peter
Nov 21 '18 at 2:42
Agreed. It took me a while to see the change here.
– adam
Nov 21 '18 at 2:43
@Peter, thanks for the help, this solved my issue. I have also corrected my usage of upper case in function names. Still learning laravel/php
– C. Gill
Nov 21 '18 at 13:22
1
@C.Gill Laravel Models have some getters that will load the relation if it exists on the Model, even if it isn't loaded yet. So if doesn't behave like an array where you would get an undefined index error when accessing a key that hadn't been set ... even though you are using array style accessors like $model['property'] to get the data.
– Peter
Nov 21 '18 at 18:44
|
show 3 more comments
Can you clarify what you changed, and why it made a difference? This will make the answer more valuable to both OP and future visitors. (BTW using theauth()
helper is is a perfectly valid way to get the auth object without having to declareuse /Illuminate/Support/Facades/Auth
or whatever. But your change to the second line was definitely needed.)
– miken32
Nov 21 '18 at 2:29
1
@miken32 Thanks for the tip on the auth() helper ... it was the upper case function/method names that were looking wrong to me, but apparently that is valid in PHP.
– Peter
Nov 21 '18 at 2:42
Agreed. It took me a while to see the change here.
– adam
Nov 21 '18 at 2:43
@Peter, thanks for the help, this solved my issue. I have also corrected my usage of upper case in function names. Still learning laravel/php
– C. Gill
Nov 21 '18 at 13:22
1
@C.Gill Laravel Models have some getters that will load the relation if it exists on the Model, even if it isn't loaded yet. So if doesn't behave like an array where you would get an undefined index error when accessing a key that hadn't been set ... even though you are using array style accessors like $model['property'] to get the data.
– Peter
Nov 21 '18 at 18:44
Can you clarify what you changed, and why it made a difference? This will make the answer more valuable to both OP and future visitors. (BTW using the
auth()
helper is is a perfectly valid way to get the auth object without having to declare use /Illuminate/Support/Facades/Auth
or whatever. But your change to the second line was definitely needed.)– miken32
Nov 21 '18 at 2:29
Can you clarify what you changed, and why it made a difference? This will make the answer more valuable to both OP and future visitors. (BTW using the
auth()
helper is is a perfectly valid way to get the auth object without having to declare use /Illuminate/Support/Facades/Auth
or whatever. But your change to the second line was definitely needed.)– miken32
Nov 21 '18 at 2:29
1
1
@miken32 Thanks for the tip on the auth() helper ... it was the upper case function/method names that were looking wrong to me, but apparently that is valid in PHP.
– Peter
Nov 21 '18 at 2:42
@miken32 Thanks for the tip on the auth() helper ... it was the upper case function/method names that were looking wrong to me, but apparently that is valid in PHP.
– Peter
Nov 21 '18 at 2:42
Agreed. It took me a while to see the change here.
– adam
Nov 21 '18 at 2:43
Agreed. It took me a while to see the change here.
– adam
Nov 21 '18 at 2:43
@Peter, thanks for the help, this solved my issue. I have also corrected my usage of upper case in function names. Still learning laravel/php
– C. Gill
Nov 21 '18 at 13:22
@Peter, thanks for the help, this solved my issue. I have also corrected my usage of upper case in function names. Still learning laravel/php
– C. Gill
Nov 21 '18 at 13:22
1
1
@C.Gill Laravel Models have some getters that will load the relation if it exists on the Model, even if it isn't loaded yet. So if doesn't behave like an array where you would get an undefined index error when accessing a key that hadn't been set ... even though you are using array style accessors like $model['property'] to get the data.
– Peter
Nov 21 '18 at 18:44
@C.Gill Laravel Models have some getters that will load the relation if it exists on the Model, even if it isn't loaded yet. So if doesn't behave like an array where you would get an undefined index error when accessing a key that hadn't been set ... even though you are using array style accessors like $model['property'] to get the data.
– Peter
Nov 21 '18 at 18:44
|
show 3 more comments
try this:
Appmodel_name::where('active_',TRUE)->where('security_level_id', $securityLevel);
As this looks nearly the same as the query given inActiveChildren
, can you explain why this one might solve the problem? What was wrong with the original query?
– Nico Haase
Nov 21 '18 at 7:31
add a comment |
try this:
Appmodel_name::where('active_',TRUE)->where('security_level_id', $securityLevel);
As this looks nearly the same as the query given inActiveChildren
, can you explain why this one might solve the problem? What was wrong with the original query?
– Nico Haase
Nov 21 '18 at 7:31
add a comment |
try this:
Appmodel_name::where('active_',TRUE)->where('security_level_id', $securityLevel);
try this:
Appmodel_name::where('active_',TRUE)->where('security_level_id', $securityLevel);
answered Nov 21 '18 at 5:11
PHP GeekPHP Geek
1,3511718
1,3511718
As this looks nearly the same as the query given inActiveChildren
, can you explain why this one might solve the problem? What was wrong with the original query?
– Nico Haase
Nov 21 '18 at 7:31
add a comment |
As this looks nearly the same as the query given inActiveChildren
, can you explain why this one might solve the problem? What was wrong with the original query?
– Nico Haase
Nov 21 '18 at 7:31
As this looks nearly the same as the query given in
ActiveChildren
, can you explain why this one might solve the problem? What was wrong with the original query?– Nico Haase
Nov 21 '18 at 7:31
As this looks nearly the same as the query given in
ActiveChildren
, can you explain why this one might solve the problem? What was wrong with the original query?– Nico Haase
Nov 21 '18 at 7:31
add a comment |
After some more investigation, I have found the issue to be in my blade code.
I still referenced $item['children']
instead of $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? Why wasn't this throwing an error?
add a comment |
After some more investigation, I have found the issue to be in my blade code.
I still referenced $item['children']
instead of $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? Why wasn't this throwing an error?
add a comment |
After some more investigation, I have found the issue to be in my blade code.
I still referenced $item['children']
instead of $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? Why wasn't this throwing an error?
After some more investigation, I have found the issue to be in my blade code.
I still referenced $item['children']
instead of $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? Why wasn't this throwing an error?
answered Nov 21 '18 at 17:09
C. GillC. Gill
238
238
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53402258%2fwhere-clause-not-filtering%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown