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.

JLZenor
  • 1,460
  • 2
  • 14
  • 23
  • `$group = Group::find($mygroup->group_id);` should be `$group = Group::find($mygroup->id);` – Anar Bayramov Sep 13 '17 at 03: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. – JLZenor Sep 13 '17 at 03:11
  • Can you share `return dd($mygroup)` inside foreach ? – Niklesh Raut Sep 13 '17 at 03: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. – JLZenor Sep 13 '17 at 03:40
  • Is it `$permission` or `$perm` ? – Niklesh Raut Sep 13 '17 at 04:11
  • It's permission, to keep the code simple I left out the $permisison query line, and realized later it could make things ambiguous. The question was answered, though. Thanks for the help everyone. – JLZenor Sep 13 '17 at 04:16

3 Answers3

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;
           }
        }
    }
     }
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
  • 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. – JLZenor Sep 13 '17 at 03:45
  • 2
    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 – Vision Coderz Sep 13 '17 at 03:49
  • That information is in the original question, but here it is again: stdClass Object ( [group_id] => 1 [permission_id] => 50 [setting] => 1 ) – JLZenor Sep 13 '17 at 03:50
  • 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. – JLZenor Sep 13 '17 at 04:16
  • glad to here answer is helped you. – Vision Coderz Sep 13 '17 at 04:16
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 :)

flauntster
  • 2,008
  • 13
  • 20
  • Nope. Just tried that and got the same Trying to get property of non-object error at the same line. – JLZenor Sep 13 '17 at 03:12
  • 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()) :) – flauntster Sep 13 '17 at 03:15
  • 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... – JLZenor Sep 13 '17 at 03:16
  • 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: https://stackoverflow.com/a/27754169/3106662 – flauntster Sep 13 '17 at 03:22
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;
      }
   }
}