Product.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Models;
  3. use Hidehalo\Nanoid\Client;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  8. use Spatie\Activitylog\LogOptions;
  9. use Spatie\Activitylog\Traits\LogsActivity;
  10. class Product extends Model
  11. {
  12. use HasFactory;
  13. use LogsActivity;
  14. public function getActivitylogOptions(): LogOptions
  15. {
  16. return LogOptions::defaults()
  17. -> logOnlyDirty()
  18. -> logOnly(['*'])
  19. -> dontSubmitEmptyLogs();
  20. }
  21. public $incrementing = false;
  22. protected $guarded = ['id'];
  23. public static function boot()
  24. {
  25. parent::boot();
  26. static::creating(function (Product $product) {
  27. $client = new Client();
  28. $product->{$product->getKeyName()} = $client->generateId($size = 21);
  29. });
  30. static::deleting(function (Product $product) {
  31. $product->nodes()->detach();
  32. $product->eggs()->detach();
  33. });
  34. }
  35. public function getHourlyPrice()
  36. {
  37. return ($this->price / 30) / 24;
  38. }
  39. public function getDailyPrice()
  40. {
  41. return $this->price / 30;
  42. }
  43. public function getWeeklyPrice()
  44. {
  45. return $this->price / 4;
  46. }
  47. /**
  48. * @return BelongsTo
  49. */
  50. public function servers()
  51. {
  52. return $this->belongsTo(Server::class, 'id', 'product_id');
  53. }
  54. /**
  55. * @return BelongsToMany
  56. */
  57. public function eggs()
  58. {
  59. return $this->belongsToMany(Egg::class);
  60. }
  61. /**
  62. * @return BelongsToMany
  63. */
  64. public function nodes()
  65. {
  66. return $this->belongsToMany(Node::class);
  67. }
  68. }