0

Simple code will not work! I keep getting the error Trying to get property of non-object on the line with the if statement. I tried accessing this like an array I get a message saying it can't access a stdClass as an Array.

 public function isAllowed($perm) { $cando = 0; $groups = DB::table('group_user')->where('user_id', $this->id)->get(); foreach ($groups as $mygroup) { $group_can = DB::table('group_permission')->where([ ['permission_id', $permission->id], ['group_id', $mygroup->group_id] ])->first(); $setting = $group_can->setting; // Error returns for this line // if ($setting > $cando) { $cando = $setting; } } } 

print_r, var_dump, and dd of $group_can give this:

stdClass Object ( [group_id] => 1 [permission_id] => 50 [setting] => 1 ) object(stdClass)#555 (3) { ["group_id"]=> int(1) ["permission_id"]=> int(50) ["setting"]=> int(1) } {#555 ▼ +"group_id": 1 +"permission_id": 50 +"setting": 1 } 

Using $setting = $group_can->setting; returns the error Trying to get property of non-object

Using $setting = $group_can['setting']; returns the error Cannot use object of type stdClass as array

The details of the laravel error are:

at HandleExceptions->handleError(8, 'Trying to get property of non-object', '/home/mwsubmissions/public_html/jon/MWSubmissionManager/app/User.php', 91, array('perm' => 'manage.projects', 'cando' => 1, 'groups' => object(Collection), 'permission' => object(stdClass), 'mygroup' => object(stdClass), 'group' => null, 'group_can' => null, 'setting' => 1))

EDIT I removed the first part of the code that I was having errors with and then got to this, another example of the same thing, but using a smaller object and this line is more important than the last was. All details updated.

6
  • $group = Group::find($mygroup->group_id); should be $group = Group::find($mygroup->id); Commented Sep 13, 2017 at 3:07
  • @AnarBayramov negative. $mygroup comes from the before mentioned $groups query, which is a pivot table with only 2 columns, group_id and user_id. The $group object is finding the appropriate data, I just can't access it. Commented Sep 13, 2017 at 3:11
  • Can you share return dd($mygroup) inside foreach ? Commented Sep 13, 2017 at 3:29
  • @user2486 I updated the question with more detail and a dd inside the foreach. I removed the previous line of code and continued on to the next, more important line of code that was returning the same error. Commented Sep 13, 2017 at 3:40
  • Is it $permission or $perm ? Commented Sep 13, 2017 at 4:11

3 Answers 3

1

Better you can check isset

public function isAllowed($perm) { $cando = 0; $groups = DB::table('group_user')->where('user_id', $this->id)->get(); if(isset($groups)&&count($groups)>0){ foreach ($groups as $mygroup) { if(isset($mygroup->group_id)){ $group = Group::find($mygroup->group_id); } if (!is_null($group->project_id)) { continue; } } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

I just tried using isset for $group_can (see my updated question) and it passed, and then I got an error as soon as I tried to access any of the data in $group_can.
i can say you are getting data in loop may be any one of the row has no data .can you print_r $group_can) and post result
That information is in the original question, but here it is again: stdClass Object ( [group_id] => 1 [permission_id] => 50 [setting] => 1 )
This was the final solution... In the for loop the first loop would go through, which is why my print_r worked. But the second iteration through is where it failed, when the $group_can query failed to return a column. Dang... 2 hours wasted on this simple thing.
glad to here answer is helped you.
0

The property you're trying to access is an element of an array 'attributes', so try:

public function isAllowed($perm) { $cando = 0; $groups = DB::table('group_user')->where('user_id', $this->id)->get(); foreach ($groups as $mygroup) { $group = Group::find($mygroup->group_id); if (!is_null($group->attributes['project_id'])) { continue; } } } 

Hope this helps :)

4 Comments

Nope. Just tried that and got the same Trying to get property of non-object error at the same line.
Sorry I just noticed the attributes array is protected.. check the class to see if there is a getter method to retrieve attributes (eg $group->getAttributes()) :)
Throughout every DB query in this application with Laravel I have accessed the same data in the same way. I cannot figure out why this one is different...
Weird, if you're accessing protected members from outside of that class. You could cast the object as an array and then access the protected members, as shown here: stackoverflow.com/a/27754169/3106662
0

First, ensure you add "project_id" to $fillable array in Group Model
Try this

public function isAllowed($perm) { $cando = 0; $groups = DB::table('group_user')->where('user_id', $this->id)->get(); foreach ($groups as $mygroup) { $group = Group::whereId($mygroup->group_id)->first(); if (!is_null($group->attributes['project_id'])) { continue; } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.