mirror of
https://github.com/brufdev/many-notes.git
synced 2026-01-23 19:38:46 -06:00
Add VaultNode model, migration and relationships
This commit is contained in:
@@ -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
68
app/Models/VaultNode.php
Normal 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,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user