F
Filament7mo ago
Lucky0

ManyToMany Relationship on AttachAction

I am trying to use AttachAction, and I am trying to implement it. But instead i got an error error:
Call to undefined method App\Models\Car\Feature::carOwners()
Call to undefined method App\Models\Car\Feature::carOwners()
model:
namespace App\Models\Car;

class Feature extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarOwner::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Models\Car;

class Feature extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarOwner::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Models;

class CarFeature extends Model
{
public function feature(): BelongsTo
{
return $this->belongsTo(CarSpec\Feature::class);
}

public function carOwner(): BelongsTo
{
return $this->belongsTo(CarOwner::class);
}
}
namespace App\Models;

class CarFeature extends Model
{
public function feature(): BelongsTo
{
return $this->belongsTo(CarSpec\Feature::class);
}

public function carOwner(): BelongsTo
{
return $this->belongsTo(CarOwner::class);
}
}
namespace App\Models;

class CarOwner extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarSpec\Feature::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Models;

class CarOwner extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarSpec\Feature::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Filament\Driver\Resources\CarOwnerResource\RelationManagers;

class CarFeaturesRelationManager extends RelationManager
{
protected static string $relationship = 'carFeatures';

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
...
}),
])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect(),
// ->form(fn (Tables\Actions\AttachAction $attachAction): array => [
// $attachAction->getRecordSelect(),
// Forms\Components\Select::make('name'),
// ]),
])
}
}
namespace App\Filament\Driver\Resources\CarOwnerResource\RelationManagers;

class CarFeaturesRelationManager extends RelationManager
{
protected static string $relationship = 'carFeatures';

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
...
}),
])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect(),
// ->form(fn (Tables\Actions\AttachAction $attachAction): array => [
// $attachAction->getRecordSelect(),
// Forms\Components\Select::make('name'),
// ]),
])
}
}
3 Replies
Lucky0
Lucky0OP7mo ago
up
LeandroFerreira
LeandroFerreira7mo ago
The issue might be that you are using a 'carOwners' method that doesn’t exist in your model
Lucky0
Lucky0OP7mo ago
I was ignoring the error. cause I was so sure that I named the function correctly.. Thank you. just literally change the carFeatures in Feature Model to 'carOwners' fixed it.
Want results from more Discord servers?
Add your server