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.
$group = Group::find($mygroup->group_id);should be$group = Group::find($mygroup->id);return dd($mygroup)inside foreach ?$permissionor$perm?