12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?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;
- use Spatie\Activitylog\LogOptions;
- use Spatie\Activitylog\Traits\LogsActivity;
- class Product extends Model
- {
- use HasFactory;
- use LogsActivity;
- public function getActivitylogOptions(): LogOptions
- {
- return LogOptions::defaults()
- -> logOnlyDirty()
- -> logOnly(['*'])
- -> dontSubmitEmptyLogs();
- }
- public $incrementing = false;
- protected $guarded = ['id'];
- public static function boot()
- {
- parent::boot();
- static::creating(function (Product $product) {
- $client = new Client();
- $product->{$product->getKeyName()} = $client->generateId($size = 21);
- });
- static::deleting(function (Product $product) {
- $product->nodes()->detach();
- $product->eggs()->detach();
- });
- }
- public function getHourlyPrice()
- {
- return ($this->price / 30) / 24;
- }
- public function getDailyPrice()
- {
- return $this->price / 30;
- }
- public function getWeeklyPrice()
- {
- return $this->price / 4;
- }
- /**
- * @return BelongsTo
- */
- public function servers()
- {
- return $this->belongsTo(Server::class, 'id', 'product_id');
- }
- /**
- * @return BelongsToMany
- */
- public function eggs()
- {
- return $this->belongsToMany(Egg::class);
- }
- /**
- * @return BelongsToMany
- */
- public function nodes()
- {
- return $this->belongsToMany(Node::class);
- }
- }
|