mardi 12 septembre 2017

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 = Group::find($mygroup->group_id);

           if (!is_null($group->project_id))
           {
              continue;
           }
        }
     }

a dd($group) shows: dd results of $group

A var_dump($group) shows

object(App\Group)#549 (25) { ["connection":protected]=> string(5) "mysql" ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(7) { ["id"]=> int(1) ["name"]=> string(6) "Admins" ["hidden"]=> int(0) ["description"]=> string(54) "Default administrator group for managing this website." ["project_id"]=> NULL ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(7) { ["id"]=> int(1) ["name"]=> string(6) "Admins" ["hidden"]=> int(0) ["description"]=> string(54) "Default administrator group for managing this website." ["project_id"]=> NULL ["created_at"]=> NULL ["updated_at"]=> NULL } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["events":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } object(stdClass)#531 (3) { ["group_id"]=> int(1) ["permission_id"]=> int(50) ["setting"]=> int(1) } NULL

which ends in NULL for some reason?

And finally the text of the Laravel error shows $group as being null as well, even though there is data there? What's going on?

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire