Product.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Spatie\Activitylog\Traits\LogsActivity;
  10. class Product extends Model
  11. {
  12. use HasFactory;
  13. use LogsActivity;
  14. public $incrementing = false;
  15. protected $guarded = ['id'];
  16. public static function boot()
  17. {
  18. parent::boot();
  19. static::creating(function (Product $product) {
  20. $client = new Client();
  21. $product->{$product->getKeyName()} = $client->generateId($size = 21);
  22. });
  23. static::deleting(function(Product $product) {
  24. $product->nodes()->detach();
  25. $product->eggs()->detach();
  26. });
  27. }
  28. public function getHourlyPrice()
  29. {
  30. return ($this->price / 30) / 24;
  31. }
  32. public function getDailyPrice()
  33. {
  34. return ($this->price / 30);
  35. }
  36. public function getWeeklyPrice()
  37. {
  38. return ($this->price / 4);
  39. }
  40. /**
  41. * @return BelongsTo
  42. */
  43. public function servers()
  44. {
  45. return $this->belongsTo(Server::class, 'id', 'product_id');
  46. }
  47. /**
  48. * @return BelongsToMany
  49. */
  50. public function eggs() {
  51. return $this->belongsToMany(Egg::class);
  52. }
  53. /**
  54. * @return BelongsToMany
  55. */
  56. public function nodes() {
  57. return $this->belongsToMany(Node::class);
  58. }
  59. }