<?php

namespace Modules\User\Models;

use App\Models\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class UserInfo extends Model
{

    protected $primaryKey = 'user_id';

    /**
     * Notes   : 用户
     *
     * @Date   : 2021/3/11 5:37 下午
     * @Author : < Jason.C >
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Notes   : 获取头像的实际地址
     *
     * @Date   : 2021/3/12 10:53 上午
     * @Author : < Jason.C >
     * @param $avatar
     * @return string
     */
    public function getAvatarAttribute($avatar): string
    {
        if (empty($avatar)) {
            $avatar = config('user.avatar');
        }

        if (Str::startsWith($avatar, 'http')) {
            return $avatar;
        }

        return $avatar ? Storage::url($avatar) : '';
    }

}
