xmrmemes/app/Models/Meme.php
dev c5141e10e0 Fix amounts from showing scientific notation
Very small amounts were showing scientific notation numbers when
dispaying the total amount of tips on a meme or user or on the
leaderboard. This should fix that.

There may be a cleaner way to do this but this gets the job done.
2021-12-10 15:20:05 -06:00

81 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\SoftDeletes;
class Meme extends Model
{
use HasFactory;
use SoftDeletes;
protected $guarded = ['id'];
protected $appends = ['meme_tips_total', 'image_url'];
protected $hidden = [
'payment_pending',
'account_index',
'is_approved',
'deleted_at',
];
protected static function booted()
{
static::addGlobalScope('approved', function (Builder $builder) {
$builder->where('is_approved', 1);
});
}
public function user()
{
return $this->belongsTo(User::class);
}
public function tips()
{
return $this->hasMany(Tip::class)->orderByDesc('created_at');
}
public function getMemeTipsTotalAttribute()
{
if ($this->tips->where('is_deposit', 1)->sum('amount_formatted')) {
return rtrim(sprintf('%.8f',floatval(
$this->tips->where('is_deposit', 1)->sum('amount_formatted')
)),'0');
} else {
return 0;
}
}
public function getImageUrlAttribute()
{
return url($this->image);
}
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;
}
}