filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.39k stars 2.97k forks source link

I could not create the tree to add categories in my Filament admin panel. can you help me? Please. #14863

Closed avrahamyosuf closed 22 hours ago

avrahamyosuf commented 22 hours ago

Package

filament/filament

Package Version

sürüm 3.0

Laravel Version

Sürüm 11

Livewire Version

No response

PHP Version

PHP 8.3

Problem description

I could not create the tree to add categories in my admin panel. can you help me? Please. image

Expected behavior

image

Steps to reproduce

`<?php

namespace App\Filament\Resources;

use App\Models\Category; use Filament\Resources\Resource; use Filament\Forms; use Filament\Forms\Form; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\Select; use Filament\Tables; use Filament\Tables\Table; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Columns\DateColumn; use Filament\Actions\EditAction; use Filament\Actions\DeleteBulkAction;

class CategoryResource extends Resource { protected static ?string $model = Category::class;

public static function form(Form $form): Form
{
    return $form
        ->schema([
            TextInput::make('ct_name')
                ->label('Kategori Adı')
                ->required(),
            TextInput::make('slug')
                ->label('Slug')
                ->required(),

            // Üst Kategori (1. Seviye)
            Select::make('parent_id')
                ->label('Üst Kategori')
                ->options(Category::whereNull('parent_id')->pluck('ct_name', 'id')->toArray())
                ->nullable()
                ->reactive()
                ->afterStateUpdated(function (callable $set, $state) {
                    $set('sub_category_id', null);
                    $set('sub_sub_category_id', null);
                    $set('sub_sub_sub_category_id', null);
                }),

            // Alt Kategori (2. Seviye)
            Select::make('sub_category_id')
                ->label('Alt Kategori')
                ->options(function (callable $get) {
                    $parentId = $get('parent_id');
                    return $parentId ? Category::where('parent_id', $parentId)->pluck('ct_name', 'id')->toArray() : [];
                })
                ->nullable()
                ->reactive()
                ->afterStateUpdated(function (callable $set, $state) {
                    $set('sub_sub_category_id', null);
                    $set('sub_sub_sub_category_id', null);
                }),

            // Alt Alt Kategori (3. Seviye)
            Select::make('sub_sub_category_id')
                ->label('Alt Alt Kategori')
                ->options(function (callable $get) {
                    $subCategoryId = $get('sub_category_id');
                    return $subCategoryId ? Category::where('parent_id', $subCategoryId)->pluck('ct_name', 'id')->toArray() : [];
                })
                ->nullable()
                ->reactive()
                ->afterStateUpdated(function (callable $set, $state) {
                    $set('sub_sub_sub_category_id', null);
                }),

            // Alt Alt Alt Kategori (4. Seviye)
            Select::make('sub_sub_sub_category_id')
                ->label('Alt Alt Alt Kategori')
                ->options(function (callable $get) {
                    $subSubCategoryId = $get('sub_sub_category_id');
                    return $subSubCategoryId ? Category::where('parent_id', $subSubCategoryId)->pluck('ct_name', 'id')->toArray() : [];
                })
                ->nullable()
                ->reactive(),
        ]);
}

public static function table(Table $table): Table
{
    return $table
        ->columns([
            TextColumn::make('id')->label('ID')->sortable(),
            TextColumn::make('parent.ct_name')->label('Üst Kategori')->sortable(),
            TextColumn::make('ct_name')->label('Kategori Adı')->sortable(),
            TextColumn::make('slug')->label('Slug')->sortable(),
            TextColumn::make('created_at')->label('Oluşturulma Tarihi')->sortable()->date(),
            TextColumn::make('updated_at')->label('Güncellenme Tarihi')->sortable()->date(),
        ])
        ->filters([
            // Filtreleme için gereksinimlerinize uygun olarak eklemeler yapabilirsiniz.
        ])
        ->actions([
            Tables\Actions\EditAction::make(),
            Tables\Actions\DeleteAction::make(),
        ])
        ->bulkActions([
            Tables\Actions\DeleteBulkAction::make(),
        ])
        ->searchable(); // Arama özelliği eklendi
}

public static function getRelations(): array
{
    return [
        // İlişkiler burada tanımlanabilir.
    ];
}

public static function getPages(): array
{
    return [
        'index' => \App\Filament\Resources\CategoryResource\Pages\ListCategories::route('/'),
        'create' => \App\Filament\Resources\CategoryResource\Pages\CreateCategory::route('/create'),
        'edit' => \App\Filament\Resources\CategoryResource\Pages\EditCategory::route('/{record}/edit'),
    ];
}

} <?php

use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;

class CreateEmlakKategorielTable extends Migration

{ public function up() { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('parent_id')->nullable(); $table->foreign('parent_id')->references('id')->on('categories')->onDelete('cascade'); $table->string('ct_name'); $table->string('slug'); $table->unsignedBigInteger('propertytype_id')->nullable(); $table->boolean('is_active')->default(true); $table->timestamps(); }); }

public function down()
{
    Schema::dropIfExists('categories');
}

`}

`<?php

namespace App\Models;

use Illuminate\Support\Str;

use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Model {

protected $table = 'categories';

protected $fillable = [
    'parent_id',
    'ct_name',
    'slug',
    'propertytype_id',
    'is_active',
];

public function children()
{
    return $this->hasMany(Category::class, 'parent_id');
}
public function parent()
{
    return $this->belongsTo(Category::class, 'parent_id');
}

// Üst kategoriye ait olan alt kategoriler

} `

github-actions[bot] commented 22 hours ago

Hey @avrahamyosuf! We're sorry to hear that you've hit this issue. 💛

However, it looks like you forgot to fill in the reproduction repository URL. Can you edit your original post and then we'll look at your issue?

We need a public GitHub repository which contains a Laravel app with the minimal amount of Filament code to reproduce the problem. Please do not link to your actual project, what we need instead is a minimal reproduction in a fresh project without any unnecessary code. This means it doesn't matter if your real project is private / confidential, since we want a link to a separate, isolated reproduction. That would allow us to download it and review your bug much easier, so it can be fixed quicker. Please make sure to include a database seeder with everything we need to set the app up quickly.