Add VaultNode model, migration and relationships

This commit is contained in:
Bruno
2024-10-03 14:08:45 +01:00
parent aec1c7751a
commit 6b319552fc
3 changed files with 111 additions and 0 deletions

View File

@@ -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);
}
}

68
app/Models/VaultNode.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
class VaultNode extends Model
{
use HasFactory;
use HasRecursiveRelationships;
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
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,
],
];
}
}

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('vault_nodes', function (Blueprint $table) {
$table->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');
}
};