mathioud
Select Field: Group Category options based on level of category
Hello, I would like to create a Select Field to display categories of products, but i want them to make sense visually like this:
- Category 1
-- SubCategory 1-1
-- SubCategory 1-2
- Category 2
-- Sub Category 2-1
-- Sub Category 2-2
-- Sub Category 2-3
and so on..
Each Category has a parent_id, pointing to the parent category.
Any ideas ? I didn't see any reference in the docs
Thank you in advance!
3 replies
Multiple Select with belongsToMany relationship, saves but does not show selected after reload.
Hello,
I have two models Model: Product and Model: Secondary Traits.
Product model has a belongsToMany relationship with SecondaryTrait just like that:
public function secondaryTraits() :belongsToMany
{
return $this->belongsToMany(SecondaryTrait::class, 'products_secondarytraits', 'product_id', 'secondaryTrait_id');
}
Also the form element used is a Multiple Select:
Select::make('secondary_traits')
->preload()
->multiple()
->relationship('secondaryTraits', 'search_as')
(the 'search_as' column is what i want the user to see in the field, because of translations in other columns)
After i select the related secondarytraits and save, they are indeed stored in the DB,
but when I refresh the page, or visit the specific product's form again, there are no options populating the field.
Any ideas ?3 replies
belongsToMany relationship between same model entries
Hello, I'm a new user of filament, already loving the capabilities, the only thing that seems to have gotten more difficult is relationship management when doing data entries.
In my case:
There is a Product model. A Product has a belongsToMany relationship with other Products. We call this relationship "relatedProducts".
There is also a "products_related" table that holds the "parent_id" and "child_id". So that when I display the product with id=1 (parent_id), the client can also see the related products(id=2,4,6,9) .
Here is the relationship: (Models/Product.php)
public function relatedProducts(){
return $this->belongsToMany(Product::class, 'product_related', 'parent_id', 'child_id')->withPivot('parent_id');
}
The Migrtion:
Schema::create('product_related', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('parent_id');
$table->unsignedBigInteger('child_id');
$table->timestamps();
})
I have made a relationManager for that called "RelatedProductsRelationManager.php"
The table (after i have populated the DB table "product_related" manually) works fine. It detaches entries nicely, too.
What i cannot seem to understand is how to make the form work, and be able to select already existing products to attach.
Taken from the RelatedProductsRelationManager.php file:
protected static string $relationship = 'relatedProducts';
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Select::make('related_product')->preload()
->options(function (RelationManager $livewire): array {
return Product::pluck('name', 'id')
->toArray();
})
]);
}
public function table(Table $table): Table
{
....
}
}
3 replies