codelyftlab's avatar

Relationship using UUID

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

0 likes
6 replies
ajagabos007's avatar

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);
 }
codelyftlab's avatar

@ajagabos007 I did have it, here is my product model

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

public function categories(): BelongsToMany
{
    return $this->belongsToMany(Category::class)->withTimestamps();
}

I havent put Category in my Product yet.

Tray2's avatar

@cnneinn You really should follow the naming conventions of laravel, that way you would not get a lot of strange errors, however this error is not that strange.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categories.id' in 'where clause'

It tells you thar there is no categories.id column in your products table, so I suggest starting there.

And reading this

https://tray2.se/posts/sqlerrm

Snapey's avatar

What version of Laravel are you using?

If you would like a model to use a UUID key instead of an auto-incrementing integer key, you may use the Illuminate\Database\Eloquent\Concerns\HasUuids trait on the model.

Have you added this?

Please or to participate in this conversation.