I tried all the answers already on the forum, none helped me.
App\\User.php Model defines hasOne relationship
public function apikey()
{
return $this->hasOne('App\ApiKey', 'user_id', 'user_id');
}
App\\ApiKey.php Model defines reverse relationship
protected function user()
{
return $this->belongsTo('App\User','user_id','user_id');
}
Now I have a transform function with is call from controller return $this->response->withItem(**$users**, new UserTransformer);
App\\Transformer\\UserTransformer.php
public function transform($resource) //$resource is a users object
{
$user_id = (int) $resource->user_id;
$apiKey = $resource->apikey->key;
return [
'user_id' => $user_id,
'apikey' => $apiKey
];
}
I get following error at $apiKey = $resource->apikey->key;
Trying to get property of non-object
I do not understand where am I going wrong Please suggest.
Thanks,
K
You should try passing just a single model instead of users.
You can use first()
or find()
when querying to get a single result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.