67 lines
1.5 KiB
PHP
67 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
/**
|
|
* @method static create(array $array)
|
|
* @method static where(string $string, string $value)
|
|
* @method static find(int $id)
|
|
* @method static orderBy(string $string)
|
|
* @property int $id
|
|
* @property string $symbol
|
|
* @property string|null $full_name
|
|
*/
|
|
class Asset extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'symbol',
|
|
'full_name',
|
|
];
|
|
|
|
protected $casts = [
|
|
'symbol' => 'string',
|
|
'full_name' => 'string',
|
|
];
|
|
|
|
public function assetPrices(): HasMany
|
|
{
|
|
return $this->hasMany(Pricing\AssetPrice::class);
|
|
}
|
|
|
|
public function users(): HasMany
|
|
{
|
|
return $this->hasMany(User::class);
|
|
}
|
|
|
|
public function currentPrice(): ?float
|
|
{
|
|
$latestPrice = $this->assetPrices()->latest('date')->first();
|
|
|
|
return $latestPrice ? $latestPrice->price : null;
|
|
}
|
|
|
|
public static function findBySymbol(string $symbol): ?self
|
|
{
|
|
return static::where('symbol', strtoupper($symbol))->first();
|
|
}
|
|
|
|
public static function findOrCreateBySymbol(string $symbol, ?string $fullName = null): self
|
|
{
|
|
$asset = static::findBySymbol($symbol);
|
|
|
|
if (! $asset) {
|
|
$asset = static::create([
|
|
'symbol' => strtoupper($symbol),
|
|
'full_name' => $fullName,
|
|
]);
|
|
}
|
|
|
|
return $asset;
|
|
}
|
|
}
|