vendredi 24 novembre 2017

Undefine property when trying to access data from another table in Laravel

I have user table and profile table. In my profile table i put the user Avatar. so i try to get those Avatar by making this function into my User Model:

public function profile(){
    return $this->hasOne('App\Profile');
}

public function getAvatar()
{
    if ($this->profile()->avatar){
        return Storage::url($this->profile->avatar);
    }else{
        return Storage::url('public/avatars/default.jpg');
    }
}

and in my blade i'm accesing the function like this:

        <img src="" class="user-image" alt="User Image">

but it give me an error "Undefined property". The user have no Avatar and i'm expecting it will get the default.jpg, so what wrong with my code? thanks.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ziphMr
via IFTTT

Aucun commentaire:

Enregistrer un commentaire