Laravel check property exist

Viewed 21789

How I can check existing property $this->team->playerAssignment->player in more rational way? Now I check it like this:

if ($this->team)
   if (($this->team->playerAssignment))
      if (($this->team->playerAssignment->player))
4 Answers

The following has always works best for me:

if(isset(Auth::user()->client->id)){
        $clientId = Auth::user()->client->id;
    }
    else{
        dump('Nothing there...');
    }

You can easily check by null coalescing operator

if ($this->team->playerAssignment->player ?? null)
Related