Last active
March 21, 2019 11:46
-
-
Save sehmbimanvir/d240f68b093d2e46c8f8a3210793f9b8 to your computer and use it in GitHub Desktop.
This is a Images Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Support\Facades\Storage; | |
class Image extends Model | |
{ | |
/* Fillable */ | |
protected $fillable = [ | |
'title', 'path', 'auth_by', 'size' | |
]; | |
/* @array $appends */ | |
public $appends = ['url', 'uploaded_time', 'size_in_kb']; | |
public function getUrlAttribute() | |
{ | |
return Storage::disk('s3')->url($this->path); | |
} | |
public function getUploadedTimeAttribute() | |
{ | |
return $this->created_at->diffForHumans(); | |
} | |
public function user() | |
{ | |
return $this->belongsTo(User::class, 'auth_by'); | |
} | |
public function getSizeInKbAttribute() | |
{ | |
return round($this->size / 1024, 2); | |
} | |
public static function boot() | |
{ | |
parent::boot(); | |
static::creating(function ($image) { | |
$image->auth_by = auth()->user()->id; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment