<?php

namespace App\Models;

use Illuminate\Support\Facades\URL;
use App\Models\ValidationModel;
use App\Models\User;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class ProfilePhoto extends ValidationModel
{
    /**
     *	Database table that maps to.
     */
    protected $table = 'profile_photos';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'size', 'filename', 'user_id'];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = ['url'];

    /**
     * The user this photo belongs to.
     *
     * @return \User
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function getUrlAttribute()
    {
        return URL::asset('photos/'.$this->attributes['filename']);
    }
}
