Create a relationship manager for a pivot table

How do I create a relationship manager for a pivot table please?
Solution:
On AmenityProperty define 2 ->belongsTo() relations (property and amenity). You should be able to access the data by property.name then. Maybe pivot.property.name or ->getStateUsing(fn ($record) => $record->pivot->property->name
Jump to solution
18 Replies
codeartisan
codeartisanOP11mo ago
The withPivot method returns an error
Dennis Koch
Dennis Koch11mo ago
Share some code and the error
codeartisan
codeartisanOP11mo ago
->withPivot(
['property_id']
)
->withPivot(
['property_id']
)
Dennis Koch
Dennis Koch11mo ago
Please a little bit more and not just 3 lines 🙈
codeartisan
codeartisanOP11mo ago
<?php

namespace App\Filament\Resources\PropertyResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class AmenitiesRelationManager extends RelationManager
{
protected static string $relationship = 'amenities';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('property_id')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('property_id')
->columns([
Tables\Columns\TextColumn::make('amenity_id'),
Tables\Columns\TextColumn::make('property_id'),
])
->filters([
//
])
->withPivot(
['property_id']
)

->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
<?php

namespace App\Filament\Resources\PropertyResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class AmenitiesRelationManager extends RelationManager
{
protected static string $relationship = 'amenities';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('property_id')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('property_id')
->columns([
Tables\Columns\TextColumn::make('amenity_id'),
Tables\Columns\TextColumn::make('property_id'),
])
->filters([
//
])
->withPivot(
['property_id']
)

->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Dennis Koch
Dennis Koch11mo ago
Not sure where you found ->withPivot() as a table method. It is not. Those are Laravel relationship methods. And it is explained in the docs:
Please ensure that any pivot attributes are listed in the withPivot() method of the relationship and inverse relationship.
codeartisan
codeartisanOP11mo ago
No description
codeartisan
codeartisanOP11mo ago
ok my bug let first check it out Right now I have added the pivot on to the modal =>
public function amenities()
{
return $this->belongsToMany(Amenity::class, "amenity_property")
->withPivot('property_id', 'amenity_id')

->withTimestamps();
}
public function amenities()
{
return $this->belongsToMany(Amenity::class, "amenity_property")
->withPivot('property_id', 'amenity_id')

->withTimestamps();
}
codeartisan
codeartisanOP11mo ago
But I want to retrieve the actual names not the ids any ideas on how best i can achieve that
No description
Dennis Koch
Dennis Koch11mo ago
Create a pivot model and define relationships on that pivot model
codeartisan
codeartisanOP11mo ago
public function amenities()
{
return $this->belongsToMany(AmenityProperty::class)
->withPivot('property_id', 'amenity_id')
->withTimestamps();
}

public function amenities()
{
return $this->belongsToMany(AmenityProperty::class)
->withPivot('property_id', 'amenity_id')
->withTimestamps();
}

My pivot modal
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;

class AmenityProperty extends Pivot
{
use HasFactory;
protected $table = 'amenity_property';
protected $fillable = [
'property_id',
'amenity_id',
];

public function properties()
{
return $this->belongsTo(Property::class, 'property_id');
}

public function amenities()
{
return $this->belongsTo(Amenity::class, 'amenity_id');
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;

class AmenityProperty extends Pivot
{
use HasFactory;
protected $table = 'amenity_property';
protected $fillable = [
'property_id',
'amenity_id',
];

public function properties()
{
return $this->belongsTo(Property::class, 'property_id');
}

public function amenities()
{
return $this->belongsTo(Amenity::class, 'amenity_id');
}
}
codeartisan
codeartisanOP11mo ago
For some reason I get this error =>
No description
codeartisan
codeartisanOP11mo ago
My Relationship Manager
<?php

namespace App\Filament\Resources\PropertyResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class AmenitiesRelationManager extends RelationManager
{
protected static string $relationship = 'amenities';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('property_id')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('property_id')
->columns([
Tables\Columns\TextColumn::make('amenity_id')
->label('Amenity'),
Tables\Columns\TextColumn::make('property_id')
->label('Property'),
])
->filters([
//
])

->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
<?php

namespace App\Filament\Resources\PropertyResource\RelationManagers;

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class AmenitiesRelationManager extends RelationManager
{
protected static string $relationship = 'amenities';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('property_id')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('property_id')
->columns([
Tables\Columns\TextColumn::make('amenity_id')
->label('Amenity'),
Tables\Columns\TextColumn::make('property_id')
->label('Property'),
])
->filters([
//
])

->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Where do you think am going wrong
Dennis Koch
Dennis Koch11mo ago
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
codeartisan
codeartisanOP11mo ago
After reading through I have made an update now
public function amenities()
{
return $this->belongsToMany(Amenity::class)
->withPivot('property_id', 'amenity_id');
}
public function amenities()
{
return $this->belongsToMany(Amenity::class)
->withPivot('property_id', 'amenity_id');
}
thats for the property modal sorry this
public function amenities()
{
return $this->belongsToMany(Amenity::class)
->using(AmenityProperty::class)
->withPivot('property_id', 'amenity_id');
}
public function amenities()
{
return $this->belongsToMany(Amenity::class)
->using(AmenityProperty::class)
->withPivot('property_id', 'amenity_id');
}
Now it does not crush as before But i need to get the actual amenity and property name
Solution
Dennis Koch
Dennis Koch11mo ago
On AmenityProperty define 2 ->belongsTo() relations (property and amenity). You should be able to access the data by property.name then. Maybe pivot.property.name or ->getStateUsing(fn ($record) => $record->pivot->property->name
codeartisan
codeartisanOP11mo ago
Thank you so much it works
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;

class AmenityProperty extends Pivot
{
use HasFactory;
protected $table = 'amenity_property';
protected $fillable = [
'property_id',
'amenity_id',
];

public function property()
{
return $this->belongsTo(Property::class);
}

public function amenity()
{
return $this->belongsTo(Amenity::class);
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;

class AmenityProperty extends Pivot
{
use HasFactory;
protected $table = 'amenity_property';
protected $fillable = [
'property_id',
'amenity_id',
];

public function property()
{
return $this->belongsTo(Property::class);
}

public function amenity()
{
return $this->belongsTo(Amenity::class);
}
}
Want results from more Discord servers?
Add your server