Search code examples
phpoctobercmsavatar

Octobercms - Getting frontend user avatar


So i'm trying to gererate a frontend user list, but i'm having trouble reaching the avatar url.

i'm able to get all user names but when i try to do the same with the profile picture the fallback is shown. The {{ user.avatar.url }} is working on the user page when someone is signed in.

I've tried to look for the query used on the backend to get the user avatar on the preview, but i was not able to find it.

I don't know if this is relevant but i'm using https://octobercms.com/plugin/netsti-uploader for frontend users to upload their avatars. It's working since if i upload it on the frontend the backend user preview shows the right avatar

This is what i am using to get all users:

CODE:

use October\Rain\Auth\Models\User;

function onInit() {
    $this['activatedUsers'] = User::whereIsActivated(true)->get();
}

MARKUP

<div>
{% for user in activatedUsers %}
    <div class="card list">
        {% if user.avatar %}
            <img class="userimg" src="{{ user.avatar.url }}">
        {% else %}
            <img class="userimg" src="assets/images/user.png">
        {% endif %}
            <p class="name"><span class="rank-title">NAME</span><br>{{ user.name }}&nbsp;{{ user.surname }}</p>
        {% if user.last_login %}
            <p><span class="rank-title">LAST UPDATE</span><br>{{ user.last_login }}</p>
        {%endif%}
    </div>
{% endfor %}

All help is appreciated, thanks


Solution

  • try to use it like that.

    use RainLab\User\Models\User;
    
    function onInit() {
        $this['activatedUsers'] = User::whereIsActivated(true)->get();
    } 
    

    Markup

    {% for user in activatedUsers %}
        <div class="card list">
        {{ user.avatar.path }}
        </div>
    {% endfor %}