F
Filament14mo ago
Kaan

Fill Form Data

Greetings! I want to perform an edit operation on my custom page I created in Filament. However, since this edit operation will be with data from a relational table, I have some difficulty in bringing the data. If I need to talk about logic briefly: There is "category_id" in categorySteps. Every data that has this "category_id" comes from the category_steps table. What I want to do here is to call the step_title of the data in the category_steps table associated with this category_id https://hastebin.skyra.pw/ezupekilon.php
Solution:
add $data and statePath ```php public ?array $data = []; ...
Jump to solution
8 Replies
Kaan
KaanOP14mo ago
<?php

namespace App\Filament\Resources\CategoryResource\Pages;

use App\Filament\Resources\CategoryResource;
use App\Models\Category;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Pages\Page;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;

use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

use Illuminate\Database\Eloquent\Model;



class ManageCategorySteps extends Page
{
protected static ?string $model = Category::class;

protected static string $resource = CategoryResource::class;

protected static string $view = 'filament.resources.category-resource.pages.manage-category-steps';

protected static ?string $title = 'Edit Category Steps';

protected static ?string $recordTitleAttribute = 'record';

protected ?int $categoryId = null;


public function mount($record): void
{
$category = Category::with('categorySteps')->findOrFail($record) ?? redirect()->back()->with('error', 'No Category Found.');
$this->categoryId = $category->id;

$this->form->fill([
'categorySteps' => $category->categorySteps->toArray(),
]);
}


public function form(Form $form): Form
{
return $form
->schema([
Repeater::make('categorySteps')
->label('Category Steps')
->schema([
TextInput::make('step_title')
->required(),
])
->columns(2)
])->columns(1);
}


protected function getFormSchema(): array
{
return [
Forms\Components\TextInput::make('name')->required(),
// ...
];
}
}
<?php

namespace App\Filament\Resources\CategoryResource\Pages;

use App\Filament\Resources\CategoryResource;
use App\Models\Category;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Pages\Page;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;

use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

use Illuminate\Database\Eloquent\Model;



class ManageCategorySteps extends Page
{
protected static ?string $model = Category::class;

protected static string $resource = CategoryResource::class;

protected static string $view = 'filament.resources.category-resource.pages.manage-category-steps';

protected static ?string $title = 'Edit Category Steps';

protected static ?string $recordTitleAttribute = 'record';

protected ?int $categoryId = null;


public function mount($record): void
{
$category = Category::with('categorySteps')->findOrFail($record) ?? redirect()->back()->with('error', 'No Category Found.');
$this->categoryId = $category->id;

$this->form->fill([
'categorySteps' => $category->categorySteps->toArray(),
]);
}


public function form(Form $form): Form
{
return $form
->schema([
Repeater::make('categorySteps')
->label('Category Steps')
->schema([
TextInput::make('step_title')
->required(),
])
->columns(2)
])->columns(1);
}


protected function getFormSchema(): array
{
return [
Forms\Components\TextInput::make('name')->required(),
// ...
];
}
}
Kaan
KaanOP14mo ago
No description
Patrick Boivin
Patrick Boivin14mo ago
I'm curious, does this need to be a custom page? Can you instead have the Repeater directly on the CategoryResource?
Kaan
KaanOP14mo ago
Yes. This page need to be custom page. Because i'm using relationship data and i'm doing custom query. So, i'm need help this scenerio
toeknee
toeknee14mo ago
Can you dd on the $category->categorySteps->toArray() I suspect you need to pluck instead of toArray and pluck the step value
Kaan
KaanOP14mo ago
But i did. Could you check my code again?
$this->form->fill([
'categorySteps' => $category->categorySteps->toArray(),
]);
$this->form->fill([
'categorySteps' => $category->categorySteps->toArray(),
]);
I think problem is in the form method.
Solution
LeandroFerreira
LeandroFerreira14mo ago
add $data and statePath
public ?array $data = [];

public function form(Form $form): Form
{
return $form
->schema([
Repeater::make('categorySteps')
->label('Category Steps')
->schema([
TextInput::make('step_title')
->required(),
])
->columns(2)
])
->columns(1)
->statePath('data');
}
public ?array $data = [];

public function form(Form $form): Form
{
return $form
->schema([
Repeater::make('categorySteps')
->label('Category Steps')
->schema([
TextInput::make('step_title')
->required(),
])
->columns(2)
])
->columns(1)
->statePath('data');
}
Kaan
KaanOP14mo ago
Thanks! It's working
Want results from more Discord servers?
Add your server