Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Models/Database.php
7432 views
1
<?php
2
3
namespace Pterodactyl\Models;
4
5
use Illuminate\Container\Container;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Factories\HasFactory;
8
use Pterodactyl\Contracts\Extensions\HashidsInterface;
9
10
/**
11
* @property int $id
12
* @property int $server_id
13
* @property int $database_host_id
14
* @property string $database
15
* @property string $username
16
* @property string $remote
17
* @property string $password
18
* @property int $max_connections
19
* @property \Carbon\Carbon $created_at
20
* @property \Carbon\Carbon $updated_at
21
* @property Server $server
22
* @property DatabaseHost $host
23
*/
24
class Database extends Model
25
{
26
/** @use HasFactory<\Database\Factories\DatabaseFactory> */
27
use HasFactory;
28
29
/**
30
* The resource name for this model when it is transformed into an
31
* API representation using fractal.
32
*/
33
public const RESOURCE_NAME = 'server_database';
34
35
/**
36
* The table associated with the model.
37
*/
38
protected $table = 'databases';
39
40
/**
41
* The attributes excluded from the model's JSON form.
42
*/
43
protected $hidden = ['password'];
44
45
/**
46
* Fields that are mass assignable.
47
*/
48
protected $fillable = [
49
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote', 'max_connections',
50
];
51
52
/**
53
* Cast values to correct type.
54
*/
55
protected $casts = [
56
'server_id' => 'integer',
57
'database_host_id' => 'integer',
58
'max_connections' => 'integer',
59
];
60
61
public static array $validationRules = [
62
'server_id' => 'required|numeric|exists:servers,id',
63
'database_host_id' => 'required|exists:database_hosts,id',
64
'database' => 'required|string|alpha_dash|between:3,48',
65
'username' => 'string|alpha_dash|between:3,100',
66
'max_connections' => 'nullable|integer',
67
'remote' => 'required|string|regex:/^[\w\-\/.%:]+$/',
68
'password' => 'string',
69
];
70
71
public function getRouteKeyName(): string
72
{
73
return $this->getKeyName();
74
}
75
76
/**
77
* Resolves the database using the ID by checking if the value provided is a HashID
78
* string value, or just the ID to the database itself.
79
*
80
* @param string|null $field
81
*
82
* @throws \Illuminate\Contracts\Container\BindingResolutionException
83
*/
84
public function resolveRouteBinding($value, $field = null): ?\Illuminate\Database\Eloquent\Model
85
{
86
if (is_scalar($value) && ($field ?? $this->getRouteKeyName()) === 'id') {
87
$value = ctype_digit((string) $value)
88
? $value
89
: Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
90
}
91
92
return $this->where($field ?? $this->getRouteKeyName(), $value)->firstOrFail();
93
}
94
95
/**
96
* Gets the host database server associated with a database.
97
*/
98
public function host(): BelongsTo
99
{
100
return $this->belongsTo(DatabaseHost::class, 'database_host_id');
101
}
102
103
/**
104
* Gets the server associated with a database.
105
*/
106
public function server(): BelongsTo
107
{
108
return $this->belongsTo(Server::class);
109
}
110
}
111
112