You should update your product model relationship to category if you have not done so.
Product model should have
public function categories(): HasMany
{
return $this->hasMany(Product::class);
}
Good evening, I have a categories table
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('name');
$table->string('slug')->unique();
$table->uuid('parent_id')
->nullable()
->constrained('categories')
->cascadeOnDelete();
$table->boolean('is_visible')->default(false);
$table->longText('description')->nullable();
$table->timestamps();
});
}
Im learning filament php and here is my CategoryResource
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Group::make()
->schema([
Forms\Components\Section::make([
Forms\Components\TextInput::make('name')
->required()
->live(onBlur: true)
->unique()
->afterStateUpdated(function (string $operation, $state, Forms\Set $set) {
if ($operation !== 'create') {
return;
}
$set('slug', Str::slug($state));
}),
Forms\Components\TextInput::make('slug')
->disabled()
->dehydrated()
->required()
->unique(Product::class, 'slug', ignoreRecord: true),
Forms\Components\MarkdownEditor::make('description')
->columnSpanFull(),
])->columns(2)
]),
Forms\Components\Group::make()
->schema([
Forms\Components\Section::make('Status')
->schema([
Forms\Components\Toggle::make('is_visible')
->label('Visibility')
->helperText('Enable or Disable Category Visibility')
->default(true),
Forms\Components\Select::make('parent_id')
->relationship('parent', 'name')
])
])
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('parent.name')
->label('Parent')
->sortable()
->searchable(),
Tables\Columns\IconColumn::make('is_visible')
->label('Visibility')
->boolean()
->sortable(),
Tables\Columns\TextColumn::make('updated_at')
->label('Updated At')
->sortable()
->date()
])
->filters([
//
])
->actions([
Tables\Actions\ActionGroup::make([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make()
]),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
Here is my category model
use HasFactory, HasUuids;
protected $fillable = [
'name',
'slug',
'parent_id',
'is_visible',
'description'
];
public function parent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}
public function child(): HasMany
{
return $this->hasMany(Category::class, 'parent_id');
}
public function products(): BelongsToMany
{
return $this->belongsToMany(Product::class);
}
Right now i have 2 Categories 'Consumables' and 'Fruit'. If i edit the 'Fruit' category and select the parent as 'Consumables' i got this error.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.id' in 'where clause'
select
count(*) as aggregate
from
`products`
where
`slug` = fruits
and `categories`.`id` <> 9a3711f9 - cfe3 -4927 - 982c - fe3ebc6d4973
But if i create a new category and set the parent category as 'Consumable' it succeed
Please or to participate in this conversation.