F
Filament2mo ago
Hasith

Convert Repeater to a Multiple Select Field

I have a 'postCategories' Pivot relationship. I already prepared the Models and Relationships as they mentioned in the Doc. The repeater works fine as expected. Is there any way to convert this Repeater field to a multiple-select field? So then it's looks cleaner than a repeater. Because i don't have extra data fields on my pivot table. Just 'post_id' and 'category_id'.
Forms\Components\Repeater::make('postCategories')
->relationship()
->schema([
Select::make('category_id')
->label('Category')
->options(Category::all()->pluck('name', 'id'))
->searchable()->preload()
->required()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
])->minItems(1)
Forms\Components\Repeater::make('postCategories')
->relationship()
->schema([
Select::make('category_id')
->label('Category')
->options(Category::all()->pluck('name', 'id'))
->searchable()->preload()
->required()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
])->minItems(1)
Solution:
The categories must be a BelongsToMany?
Jump to solution
12 Replies
Tally
Tally2mo ago
You have a post and you want to select multiple categories? If that's the case a CheckboxList would be a nice solution as well https://filamentphp.com/docs/3.x/forms/fields/checkbox-list but if you have lots of categories maybe use 1 select with multiple options like this https://filamentphp.com/docs/3.x/forms/fields/select#multi-select
Hasith
Hasith2mo ago
Select::make('categories')
->label('Category')
->relationship('categories')
->multiple()
->searchable()->preload()->required(),
Select::make('categories')
->label('Category')
->relationship('categories')
->multiple()
->searchable()->preload()->required(),
This is my form input fiedl and it's give me an error saying SQLSTATE[42S22]: Column not found: 1054 Unknown column 'post_categories.' in 'field list'. This is my Model Configurations to my Pivot table using a pivot Model. @Tally In my Post Model
public function categories(): HasMany
{
return $this->hasMany(PostCategory::class, 'post_id');
}
public function categories(): HasMany
{
return $this->hasMany(PostCategory::class, 'post_id');
}
In my Pivot Table Model (PostCategory)
<?php

namespace App\Models;

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

class PostCategory extends Pivot
{
use HasFactory;

protected $table = 'post_categories';

public $timestamps = false;

protected $fillable = ['post_id', 'category_id'];

public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}

public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
}
<?php

namespace App\Models;

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

class PostCategory extends Pivot
{
use HasFactory;

protected $table = 'post_categories';

public $timestamps = false;

protected $fillable = ['post_id', 'category_id'];

public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
}

public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
}
When creating a Post i want to let the user to select multiple categories using a select field.
Solution
Tally
Tally2mo ago
The categories must be a BelongsToMany?
Tally
Tally2mo ago
wait... you don't need the PostCategory pivot at all if you change the categories() to a BelongsToMany of your Category::class it will work I only needed to specify a title in my relationship to get it to work:
->relationship('categories', 'title'),
->relationship('categories', 'title'),
Tally
Tally2mo ago
I see it... never needed it though 😉
Hasith
Hasith2mo ago
Let me try your solution fast
Tally
Tally2mo ago
I'm not really using it with a repeater... was using the CheckboxList... converted it to a Select and it worked
Hasith
Hasith2mo ago
It's work fine bro. How can i show that details on Post table as badges? Can you help me with that too... This works for me
public function categories(): BelongsToMany
{
return $this->belongsToMany(Category::class, 'post_categories', 'post_id', 'category_id');
}
public function categories(): BelongsToMany
{
return $this->belongsToMany(Category::class, 'post_categories', 'post_id', 'category_id');
}
Tally
Tally2mo ago
Tables\Columns\TextColumn::make('categories.name')
->label('Categories')
->badge(),
Tables\Columns\TextColumn::make('categories.name')
->label('Categories')
->badge(),
Hasith
Hasith2mo ago
Got that too. Thanks in advace 💌 @Tally
Tally
Tally2mo ago
you're welcome!