Skip to content

Instantly share code, notes, and snippets.

@sehmbimanvir
Last active March 21, 2019 11:46
Show Gist options
  • Save sehmbimanvir/d240f68b093d2e46c8f8a3210793f9b8 to your computer and use it in GitHub Desktop.
Save sehmbimanvir/d240f68b093d2e46c8f8a3210793f9b8 to your computer and use it in GitHub Desktop.
This is a Images Model
<?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