2021-07-16 02:35:54 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-07-26 22:39:11 -04:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2021-07-16 02:35:54 -04:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2021-07-26 22:39:11 -04:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2021-07-16 02:35:54 -04:00
|
|
|
|
|
|
|
class Meme extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
2021-07-26 22:39:11 -04:00
|
|
|
use SoftDeletes;
|
2021-07-16 02:35:54 -04:00
|
|
|
|
|
|
|
protected $guarded = ['id'];
|
2021-08-06 16:06:07 -04:00
|
|
|
protected $appends = ['meme_tips_total', 'image_url'];
|
|
|
|
protected $hidden = [
|
|
|
|
'payment_pending',
|
|
|
|
'account_index',
|
|
|
|
'is_approved',
|
|
|
|
'deleted_at',
|
|
|
|
];
|
|
|
|
|
2021-07-16 02:35:54 -04:00
|
|
|
|
2021-07-26 22:39:11 -04:00
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::addGlobalScope('approved', function (Builder $builder) {
|
|
|
|
$builder->where('is_approved', 1);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-16 02:35:54 -04:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tips()
|
|
|
|
{
|
2021-08-06 16:06:07 -04:00
|
|
|
return $this->hasMany(Tip::class)->orderByDesc('created_at');
|
2021-07-16 02:35:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getMemeTipsTotalAttribute()
|
|
|
|
{
|
2021-07-26 22:39:11 -04:00
|
|
|
return $this->tips->where('is_deposit', 1)->sum('amount_formatted');
|
2021-07-16 02:35:54 -04:00
|
|
|
}
|
|
|
|
|
2021-08-06 16:06:07 -04:00
|
|
|
public function getImageUrlAttribute()
|
|
|
|
{
|
|
|
|
return url($this->image);
|
|
|
|
}
|
|
|
|
|
2021-07-16 02:35:54 -04:00
|
|
|
public function setImageAttribute($value)
|
|
|
|
{
|
|
|
|
$attribute_name = "image";
|
|
|
|
$disk = "uploads";
|
|
|
|
$destination_path = "uploads/memes";
|
|
|
|
$image = \Image::make($value)->encode($value->extension(), 90);
|
|
|
|
$fix_rotation_issues = $image->orientate();
|
|
|
|
$filename = md5($value.time()) . '.' . $value->extension();
|
|
|
|
|
|
|
|
if ($value->extension() == 'gif') {
|
|
|
|
// Work around to get GIFs to work
|
|
|
|
copy($value->getRealPath(), $destination_path.'/'.$filename);
|
|
|
|
$image->destroy();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
\Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->attributes[$attribute_name] = $destination_path.'/'.$filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|