MorphMany and RelationManager wit related Resource
Hello together,
Currently I struggel with a relation manager. I have multiple Services (like Wordpress, Laravel and more). Those services can be attached to multiple domains. On the domain name is a unique constraint to make sure there are nu duplicates. I ccurrently try to use the relation manager like this:
<?php
namespace App\Filament\Resources\ServiceOdooResource\RelationManagers;
use App\Filament\Resources\HostnameResource;
use Filament\Actions\CreateAction;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Table;
class HostnamesRelationManager extends RelationManager
{
protected static string $relationship = 'hostnames';
protected static ?string $relatedResource = HostnameResource::class;
public function table(Table $table): Table
{
return $table
->headerActions([
CreateAction::make(),
]);
}
}
<?php
namespace App\Filament\Resources\ServiceOdooResource\RelationManagers;
use App\Filament\Resources\HostnameResource;
use Filament\Actions\CreateAction;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Table;
class HostnamesRelationManager extends RelationManager
{
protected static string $relationship = 'hostnames';
protected static ?string $relatedResource = HostnameResource::class;
public function table(Table $table): Table
{
return $table
->headerActions([
CreateAction::make(),
]);
}
}
3 Replies
The models look like this:
If I try to create a hostname from the service it did not get attached. Does anybody have an idea how I can solve this?
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class Hostname extends Model
{
use SoftDeletes;
protected $fillable = [
'name',
'description',
'hostnameable_id',
'hostnameable_type',
];
public function hostnameable(): MorphTo
{
return $this->morphTo();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class Hostname extends Model
{
use SoftDeletes;
protected $fillable = [
'name',
'description',
'hostnameable_id',
'hostnameable_type',
];
public function hostnameable(): MorphTo
{
return $this->morphTo();
}
}
<?php
namespace App\Models;
use App\Actions\ServiceOdoo\DeleteService;
use App\Actions\ServiceOdoo\UpdateOrCreateService;
use App\Enums\Projects\OdooTypes;
use App\Enums\Projects\OdooVersions;
use App\Models\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
class ServiceOdoo extends Model
{
protected $fillable = [
'name',
'description',
'server_id',
'version',
'admin_user',
'admin_password',
'type',
'is_running',
'webhook_token',
'postgres_password',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<\App\Models\Hostname, $this>
*/
public function hostnames(): MorphMany
{
return $this->morphMany(Hostname::class, 'hostnameable');
}
}
<?php
namespace App\Models;
use App\Actions\ServiceOdoo\DeleteService;
use App\Actions\ServiceOdoo\UpdateOrCreateService;
use App\Enums\Projects\OdooTypes;
use App\Enums\Projects\OdooVersions;
use App\Models\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
class ServiceOdoo extends Model
{
protected $fillable = [
'name',
'description',
'server_id',
'version',
'admin_user',
'admin_password',
'type',
'is_running',
'webhook_token',
'postgres_password',
];
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<\App\Models\Hostname, $this>
*/
public function hostnames(): MorphMany
{
return $this->morphMany(Hostname::class, 'hostnameable');
}
}
Give the Table and Form Code to see what you have there
I guess you need to use -
php
$form->headerActions([
// ...
AttachAction::make(),
])
php
$form->headerActions([
// ...
AttachAction::make(),
])