ctrlpanel/app/Models/Product.php

83 lines
1.7 KiB
PHP
Raw Normal View History

2021-06-05 09:26:32 +00:00
<?php
namespace App\Models;
use Hidehalo\Nanoid\Client;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2023-01-05 18:33:46 +00:00
use Spatie\Activitylog\LogOptions;
2021-06-05 09:26:32 +00:00
use Spatie\Activitylog\Traits\LogsActivity;
class Product extends Model
{
2021-11-06 00:56:57 +00:00
use HasFactory;
use LogsActivity;
2023-01-05 18:33:46 +00:00
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
-> logOnlyDirty()
-> logOnly(['*'])
-> dontSubmitEmptyLogs();
}
2021-06-05 09:26:32 +00:00
public $incrementing = false;
protected $guarded = ['id'];
2021-10-01 21:21:49 +00:00
public static function boot()
{
2021-06-05 09:26:32 +00:00
parent::boot();
2021-10-01 21:21:49 +00:00
static::creating(function (Product $product) {
2021-06-05 09:26:32 +00:00
$client = new Client();
$product->{$product->getKeyName()} = $client->generateId($size = 21);
});
static::deleting(function (Product $product) {
$product->nodes()->detach();
$product->eggs()->detach();
});
2021-06-05 09:26:32 +00:00
}
2021-06-25 21:42:53 +00:00
public function getHourlyPrice()
{
return ($this->price / 30) / 24;
2021-06-25 21:42:53 +00:00
}
public function getDailyPrice()
{
return $this->price / 30;
2021-06-25 21:42:53 +00:00
}
public function getWeeklyPrice()
{
return $this->price / 4;
2021-06-25 21:42:53 +00:00
}
2021-06-05 09:26:32 +00:00
/**
* @return BelongsTo
*/
public function servers()
2021-06-05 09:26:32 +00:00
{
2021-10-01 21:21:49 +00:00
return $this->belongsTo(Server::class, 'id', 'product_id');
2021-06-05 09:26:32 +00:00
}
/**
* @return BelongsToMany
*/
public function eggs()
{
return $this->belongsToMany(Egg::class);
}
/**
* @return BelongsToMany
*/
public function nodes()
{
return $this->belongsToMany(Node::class);
}
2021-06-05 09:26:32 +00:00
}