70208d2157
Shift automatically applies the Laravel coding style - which uses the PSR-12 coding style as a base with some minor additions. You may customize the code style applied by configuring [Pint](https://laravel.com/docs/pint), [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer), or [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) for your project root. For more information on customizing the code style applied by Shift, [watch this short video](https://laravelshift.com/videos/shift-code-style).
76 lines
1.5 KiB
PHP
76 lines
1.5 KiB
PHP
<?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\Traits\LogsActivity;
|
|
|
|
class Product extends Model
|
|
{
|
|
use HasFactory;
|
|
use LogsActivity;
|
|
|
|
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);
|
|
}
|
|
}
|