JLZenor
JLZenor

Reputation: 1480

Laravel trying to get property of non-object in an object

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.

Upvotes: 0

Views: 4836

Answers (3)

Oyewole A. Samuel
Oyewole A. Samuel

Reputation: 233

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;
      }
   }
}

Upvotes: 0

Vision Coderz
Vision Coderz

Reputation: 8078

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;
           }
        }
    }
     }

Upvotes: 1

flauntster
flauntster

Reputation: 2016

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 :)

Upvotes: 0

Related Questions