Skip to content

Commit 5b61c9d

Browse files
committed
Migrations and model
1 parent e4c3f05 commit 5b61c9d

File tree

6 files changed

+102
-9
lines changed

6 files changed

+102
-9
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"require": {
2323
"php": "^8.1",
2424
"filament/filament": "^3.0",
25-
"spatie/laravel-package-tools": "^1.15.0"
25+
"spatie/laravel-package-tools": "^1.15.0",
26+
"saade/filament-adjacency-list": "^3.2",
27+
"staudenmeir/laravel-adjacency-list": "^1.0"
2628
},
2729
"require-dev": {
2830
"laravel/pint": "^1.0",

config/fields.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

3-
// config for Vormkracht10/Fields
43
return [
54

6-
];
5+
'is_tenant_aware' => true,
6+
7+
'tenant_ownership_relationship_name' => 'tenant',
8+
9+
'tenant_relationship' => 'tenant',
10+
11+
// 'tenant_model' => \App\Models\Tenant::class,
12+
];

database/migrations/create_fields_table.php.stub

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,34 @@ return new class extends Migration
88
{
99
public function up()
1010
{
11-
Schema::create('filament_fields_table', function (Blueprint $table) {
12-
$table->id();
11+
Schema::create('fields_table', function (Blueprint $table) {
12+
$table->ulid('ulid')->primary();
13+
$table->ulid('parent_ulid')->nullable();
1314

14-
// add fields
15+
$table->string('model_type');
16+
$table->string('model_key');
17+
18+
$table->string('slug');
19+
20+
$table->string('name');
21+
$table->string('field_type');
22+
$table->json('config')->nullable();
23+
24+
$table->unsignedInteger('position')->default(0);
1525

1626
$table->timestamps();
27+
28+
$table->unique(['model_type', 'model_key', 'slug']);
1729
});
30+
31+
if (config('fields.is_tenant_aware')) {
32+
$tenant = config('fields.tenant_relationship');
33+
Schema::table('field_' . $tenant . '_table', function (Blueprint $table) use ($tenant) {
34+
$table->foreignUlid($tenant . '_ulid')->constrained(table: $tenant . '_table', column: 'ulid')->cascadeOnUpdate()->cascadeOnDelete();
35+
$table->foreignUlid('field_ulid')->constrained(table: 'fields_table', column: 'ulid')->cascadeOnUpdate()->cascadeOnDelete();
36+
37+
$table->index(['field_ulid', $tenant . '_ulid']);
38+
});
39+
}
1840
}
1941
};

src/FieldsServiceProvider.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Spatie\LaravelPackageTools\Commands\InstallCommand;
1414
use Spatie\LaravelPackageTools\Package;
1515
use Spatie\LaravelPackageTools\PackageServiceProvider;
16-
use Vormkracht10\Fields\Commands\FieldsCommand;
1716
use Vormkracht10\Fields\Testing\TestsFields;
1817

1918
class FieldsServiceProvider extends PackageServiceProvider
@@ -112,7 +111,6 @@ protected function getAssets(): array
112111
protected function getCommands(): array
113112
{
114113
return [
115-
FieldsCommand::class,
116114
];
117115
}
118116

src/Models/Field.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,51 @@
22

33
namespace Vormkracht10\FilamentFields\Models;
44

5+
use Illuminate\Support\Facades\Config;
56
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Concerns\HasUlids;
8+
use Illuminate\Database\Eloquent\Relations\HasMany;
9+
use Illuminate\Database\Eloquent\Relations\MorphTo;
10+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
11+
use Vormkracht10\FilamentFields\Shared\HasPackageFactory;
12+
use Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
613

7-
class Field extends Model {}
14+
class Field extends Model
15+
{
16+
use HasPackageFactory;
17+
use HasUlids;
18+
use HasRecursiveRelationships;
19+
20+
protected $primaryKey = 'ulid';
21+
22+
protected $guarded = [];
23+
24+
protected function casts(): array
25+
{
26+
return [
27+
'config' => 'array',
28+
];
29+
}
30+
31+
public function model(): MorphTo
32+
{
33+
return $this->morphTo('model', 'model_type', 'model_key', 'slug');
34+
}
35+
36+
public function children(): HasMany
37+
{
38+
return $this->hasMany(Field::class, 'parent_ulid')->with('children')->orderBy('position');
39+
}
40+
41+
public function tenant(): ?BelongsTo
42+
{
43+
$tenantRelationship = Config::get('fields.tenant_relationship');
44+
$tenantModel = Config::get('fields.tenant_model');
45+
46+
if ($tenantRelationship && class_exists($tenantModel)) {
47+
return $this->belongsTo($tenantModel, $tenantRelationship . '_ulid');
48+
}
49+
50+
return null;
51+
}
52+
}

src/Shared/HasPackageFactory.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Vormkracht10\FilamentFields\Shared;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Support\Str;
7+
8+
class HasPackageFactory
9+
{
10+
use HasFactory;
11+
12+
protected static function newFactory()
13+
{
14+
$package = Str::before(get_called_class(), 'Models\\');
15+
$modelName = Str::after(get_called_class(), 'Models\\');
16+
$path = $package . 'Database\\Factories\\' . $modelName . 'Factory';
17+
18+
return $path::new();
19+
}
20+
}

0 commit comments

Comments
 (0)