From 6b319552fcbbdf56e36a4abbded6dd9f29caef79 Mon Sep 17 00:00:00 2001 From: Bruno Date: Thu, 3 Oct 2024 14:08:45 +0100 Subject: [PATCH] Add VaultNode model, migration and relationships --- app/Models/Vault.php | 9 +++ app/Models/VaultNode.php | 68 +++++++++++++++++++ ..._08_16_010335_create_vault_nodes_table.php | 34 ++++++++++ 3 files changed, 111 insertions(+) create mode 100644 app/Models/VaultNode.php create mode 100644 database/migrations/2024_08_16_010335_create_vault_nodes_table.php diff --git a/app/Models/Vault.php b/app/Models/Vault.php index 6b8b1ea..a375905 100644 --- a/app/Models/Vault.php +++ b/app/Models/Vault.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Models\User; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -29,4 +30,12 @@ class Vault extends Model { return $this->belongsTo(User::class, 'created_by'); } + + /** + * Get the nodes for the vault. + */ + public function nodes(): HasMany + { + return $this->hasMany(VaultNode::class); + } } diff --git a/app/Models/VaultNode.php b/app/Models/VaultNode.php new file mode 100644 index 0000000..857ae68 --- /dev/null +++ b/app/Models/VaultNode.php @@ -0,0 +1,68 @@ + + */ + protected function casts(): array + { + return [ + 'is_file' => 'boolean', + ]; + } + + /** + * The attributes that are mass assignable. + * + * @var string[] + */ + protected $fillable = [ + 'parent_id', + 'is_file', + 'name', + 'extension', + 'content', + ]; + + /** + * Get the associated vault. + */ + public function vault(): BelongsTo + { + return $this->belongsTo(Vault::class); + } + + /** + * Get the nodes for the vault. + */ + public function childs(): HasMany + { + return $this->hasMany(VaultNode::class, 'parent_id'); + } + + public function getCustomPaths(): array + { + return [ + [ + 'name' => 'full_path', + 'column' => 'name', + 'separator' => '/', + 'reverse' => true, + ], + ]; + } +} diff --git a/database/migrations/2024_08_16_010335_create_vault_nodes_table.php b/database/migrations/2024_08_16_010335_create_vault_nodes_table.php new file mode 100644 index 0000000..84392cb --- /dev/null +++ b/database/migrations/2024_08_16_010335_create_vault_nodes_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('vault_id')->constrained('vaults'); + $table->foreignId('parent_id')->nullable()->constrained('vault_nodes'); + $table->unsignedTinyInteger('is_file'); + $table->string('name'); + $table->string('extension')->nullable(); + $table->mediumText('content')->nullable(); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('vault_nodes'); + } +};