E
E
Emil Rakhmatullin2019-10-14 11:42:42
Laravel
Emil Rakhmatullin, 2019-10-14 11:42:42

Getting user via AJAX in Laravel. How can I display his avatar?

I add the user to a tape through AJAX. By default, I display it like this: {{ Storage::url( $user->avatar ) }}. However, through AJAX this no longer works -_-

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Kylin, 2019-10-14
@palkan_karabov

Form the full path to the avatar on the backend, and then just substitute the path.
eloquent serialization

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['avatar_url'];
   
    public function getAvatarUrlAttribute()
    {
        return \Storage::url( $this->avatar );
    }
}

Y
Yuri Kulaxyz, 2019-10-14
@Kulaxyz

Yes, just write the path as it is displayed in the browser and that's it, for example: '/public/storage/avatars/' +user.avatar

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question