Files
pterodactyl-panel/app/Models/Database.php
T

98 lines
2.6 KiB
PHP
Raw 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 Carbon\Carbon;
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
/**
* @property int $id
* @property int $server_id
* @property int $database_host_id
* @property string $database
* @property string $username
* @property string $remote
* @property string $password
* @property int $max_connections
2026-10-05 18:10:18 -04:00
* @property Carbon $created_at
* @property Carbon $updated_at
2024-10-21 19:18:20 -06:00
* @property Server $server
* @property 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;
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
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
/**
* Gets the host database server associated with a database.
*
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
/**
* Gets the server associated with a database.
*
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
}