Support the ongoing development of Laravel.io →
Architecture
Last updated 1 year ago.
0

Why are you doing a foreach in your controller normally the foreach is going to be done in the view to display the data. Show more of your code it doesn't even look like you are doing a query. Look at the docs once more at has many or one too many look at the example.
Also you may want to view some Laracast on this subject.

0

jimgwhit said:

Why are you doing a foreach in your controller normally the foreach is going to be done in the view to display the data. Show more of your code it doesn't even look like you are doing a query. Look at the docs once more at has many or one too many look at the example.
Also you may want to view some Laracast on this subject.

This is fine, the code is fine. It's just that it isn't returning the $role, it's just skipping over it, even though it is being triggered because I placed dd($role) before the return and it was being triggered.

0

You need to change it to else return false the way your code is now it will always return false I just recognized that. sorry I meant to put a condition there, because returning false is the last thing the function sees therefore always false.

I don't follow your foreach. Is there only one pass, or multiple passes. In result have you tryed an associative result rather that object? Could you show more code, where this is called from and where the results go?

Last updated 8 years ago.
0

I did a test, the function return is correct, but without seeing your other code, it's hard to figure out.

0

I did a test, the function return is correct, but without seeing your other code, it's hard to figure out.

0

@CraftThatBlock,

I don't see any errors in the two code snippets you posted, the error is most likely elsewhere.

Add \Log::debug("role:$role"); before return in getRole(), and another debug log after $role = $organization->getRole($request->user);

I can't imagine a situation where a function returns a value which is suddenly false on the next line. The only thing I can think of is that getRole is overridden in a subclass and it calls parent's getRole but then returns false instead of parent's result. You can check for this by throwing an Exception before return and examining the call stack.

Last updated 8 years ago.
0

Xum said:

@CraftThatBlock,

I don't see any errors in the two code snippets you posted, the error is most likely elsewhere.

Add \Log::debug("role:$role"); before return in getRole(), and another debug log after $role = $organization->getRole($request->user);

I can't imagine a situation where a function returns a value which is suddenly false on the next line. The only thing I can think of is that getRole is overridden in a subclass and it calls parent's getRole but then returns false instead of parent's result. You can check for this by throwing an Exception before return and examining the call stack.

public function getRole(User $user)
{
	$role = false;
	foreach ($this->organization_links_loaded() as $link) {
		if ($link->user_id === $user->id) {
			\Log::debug("role1:$role");
			$role = $link->role;
			\Log::debug("role2:$role");
		}
	}

	return $role;
}

--

$role = $organization->getRole($request->user);
 \Log::debug("role3:$role");

Somehow outputs

[2015-04-27 15:14:54] local.DEBUG: role1:  
[2015-04-27 15:14:54] local.DEBUG: role2:OWNER  
[2015-04-27 15:14:54] local.DEBUG: role3:  
Last updated 8 years ago.
0

Try to throw an Exception instead of second debug log and post the call stack, maybe it's going to clear up this bizarre phenomenon.

0

Also can you try

 if ($link->user_id === $user->id) {

To

 if ($link->user_id == $user->id) {
Last updated 8 years ago.
0

jimgwhit said:

Also can you try

if ($link->user_id === $user->id) {

To

if ($link->user_id == $user->id) {

Same results.

0

Let us know if you figure this out, I am curious, and I bet Xum is to.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.