Reputation: 1339
I have a problem. I want to get user image link from DB but I'm getting nothing. Just blank space. I don't know why this is happening because other attributes are showing. Heres the code:
forum.integration.php
/*
|--------------------------------------------------------------------------
| Application user model
|--------------------------------------------------------------------------
|
| The user model from the main application
|
*/
'user_model' => 'App\User',
/*
|--------------------------------------------------------------------------
| Application user name attribute
|--------------------------------------------------------------------------
|
| The attribute on the user model to use as a display name.
|
*/
'user_name_attribute' => 'name',
'user_role_attribute' => 'role',
'user_image_attribute' => 'image',
HasAuthor.php
public function author()
{
return $this->belongsTo(config('forum.integration.user_model'), 'author_id');
}
public function getAuthorNameAttribute()
{
$attribute = config('forum.integration.user_name_attribute');
if (!is_null($this->author) && isset($this->author->$attribute)) {
return $this->author->$attribute;
}
return NULL;
}
public function getAuthorRoleAttribute()
{
$attribute = config('forum.integration.user_role_attribute');
return $this->author->$attribute;
}
public function getAuthorImageAttribute()
{
$attribute = config('forum.integration.user_image_attribute');
return $this->author->attribute;
}
I'm getting this fine -> $post->authorRole
Also I'm getting this fine -> $post->authorName
But when I do $post->authorImage
I get nothing. No errors, just blank space.
Upvotes: 0
Views: 55
Reputation: 1339
I forgot to add $
sign to this line:
public function getAuthorImageAttribute()
{
$attribute = config('forum.integration.user_image_attribute');
return $this->author->**$**attribute;
}
Upvotes: 0
Reputation: 5267
Your function is missing a $
. It should be like this:
public function getAuthorImageAttribute()
{
$attribute = config('forum.integration.user_image_attribute');
return $this->author->$attribute;
}
Note the extra $
before attribute
in the last line.
Upvotes: 1