Files

78 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2016-02-08 18:03:02 -05:00
<?php
2016-12-07 22:46:38 +00:00
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2016-02-08 18:03:02 -05:00
namespace Pterodactyl\Models;
2026-10-05 18:10:18 -04:00
use Database\Factories\DatabaseFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Hidden;
2024-10-21 19:18:20 -06:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2026-10-05 18:10:18 -04:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Pterodactyl\Facades\Hashids;
2022-05-22 14:10:01 -04:00
/**
2026-10-11 22:55:07 -04:00
* @property-read DatabaseHost $host
*/
2026-10-05 18:10:18 -04:00
#[Fillable([
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote', 'max_connections',
])]
#[Hidden(['password'])]
class Database extends Model
2016-02-08 18:03:02 -05:00
{
2026-10-05 18:10:18 -04:00
/** @use HasFactory<DatabaseFactory> */
2024-10-21 19:18:20 -06:00
use HasFactory;
2026-10-05 18:10:18 -04:00
public const string RESOURCE_NAME = 'server_database';
2016-02-08 18:03:02 -05:00
2017-03-19 19:36:50 -04:00
/**
2026-10-05 18:10:18 -04:00
* @return array<string, string>
2017-03-19 19:36:50 -04:00
*/
2026-10-05 18:10:18 -04:00
protected function casts(): array
{
return [
'server_id' => 'integer',
'database_host_id' => 'integer',
'max_connections' => 'integer',
];
}
2017-07-15 11:52:34 -05:00
2022-05-22 14:10:01 -04:00
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
* Resolves the database using the ID by checking if the value provided is a HashID
* string value, or just the ID to the database itself.
*
2026-10-05 18:10:18 -04:00
* @param string|null $field
2022-05-22 14:10:01 -04:00
*/
2022-10-14 10:59:20 -06:00
public function resolveRouteBinding($value, $field = null): ?\Illuminate\Database\Eloquent\Model
2022-05-22 14:10:01 -04:00
{
2026-10-05 18:10:18 -04:00
if (($field ?? $this->getRouteKeyName()) === 'id' && (is_string($value) || is_int($value))) {
$value = is_int($value) || ctype_digit($value)
2022-05-22 14:10:01 -04:00
? $value
2026-10-05 18:10:18 -04:00
: Hashids::decodeFirst($value);
2022-05-22 14:10:01 -04:00
}
return $this->where($field ?? $this->getRouteKeyName(), $value)->firstOrFail();
}
2017-03-19 19:36:50 -04:00
/**
2026-10-05 18:10:18 -04:00
* @return BelongsTo<DatabaseHost, $this>
2017-03-19 19:36:50 -04:00
*/
2022-10-14 10:59:20 -06:00
public function host(): BelongsTo
2017-03-19 19:36:50 -04:00
{
return $this->belongsTo(DatabaseHost::class, 'database_host_id');
}
2017-03-19 19:36:50 -04:00
/**
2026-10-05 18:10:18 -04:00
* @return BelongsTo<Server, $this>
2017-03-19 19:36:50 -04:00
*/
2022-10-14 10:59:20 -06:00
public function server(): BelongsTo
2017-03-19 19:36:50 -04:00
{
return $this->belongsTo(Server::class);
}
2016-02-08 18:03:02 -05:00
}