Hey guys,
I am hassling with some JsonResource and did not find any relevant information on that topic.
First some information about the planned implementation:
The resource looks like this:
class User extends Resource
{
public function toArray($request)
{
return [
'id' => $this->_id,
'email' => $this->email,
'firstName' => $this->first_name,
'lastName' => $this->last_name,
];
}
}
When I fetch all users of a specific workspace from taskworld, I get the following error:
ErrorException
Undefined property: stdClass::$first_name
According to the api they should define the 4 fields (_id, email, first_name, last_name), but my tests show one system-user without first_name and last_name. That will be the reason, why the exception is triggered.
I already tried to check for existence of a property and to alter the Method 'toArray' like this:
class User extends Resource
{
public function toArray($request)
{
if(!defined($this->first_name) && !defined($this->last_name)){
return [
'id' => $this->_id,
'email' => $this->email,
'first_name' => null,
'last_name' => null,
];
}else{
return [
'id' => $this->_id,
'email' => $this->email,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
];
}
}
}
That does also trigger the exception.
How can I prevent this behaviour?
Thanks in advance :-)
Kind regards, Cris