Why does TextInput does not want to save value

Hi everyone i have a problem where mine textinput does not want to save the value i enter. This is how i made the textinput

public static function form(Form $form): Form
{
return $form
->schema(Project::getForm())->columns(1);
}
// admin panel form
public static function getForm():array
{
return [
Tabs::make()->schema(Project::makeTabs()),

private static function makeTabs()
{
$tabs = [];
$languages = Languages::cases();
foreach($languages as $lang)
{
$tabs[] = Tab::make(Languages::getFullName($lang->name))
->schema([
TextInput::make("contents.{$lang->value}.title")
->required()
->maxLength(50)
->afterStateHydrated(function (TextInput $component, $state, $record) use ($lang) {
if($record){
$content = $record->contents()->language($lang->value)->first();
$component->state($content->title ?? null);
}
})

public static function form(Form $form): Form
{
return $form
->schema(Project::getForm())->columns(1);
}
// admin panel form
public static function getForm():array
{
return [
Tabs::make()->schema(Project::makeTabs()),

private static function makeTabs()
{
$tabs = [];
$languages = Languages::cases();
foreach($languages as $lang)
{
$tabs[] = Tab::make(Languages::getFullName($lang->name))
->schema([
TextInput::make("contents.{$lang->value}.title")
->required()
->maxLength(50)
->afterStateHydrated(function (TextInput $component, $state, $record) use ($lang) {
if($record){
$content = $record->contents()->language($lang->value)->first();
$component->state($content->title ?? null);
}
})
3 Replies
mile4841
mile4841OP4mo ago
Here you can see the code in better structure
No description
Marco
Marco4mo ago
The name of the textInput = contents.{$lang->value}.title The save will try to resolve the relation contents.[lang] right? and save it to the title column on that relation. Does that relation even exist in the contents model?
mile4841
mile4841OP4mo ago
I have a table called project_contents and table projects, i have a relation between Project and ProjecContent
public function contents(): HasMany
{
return $this->hasMany(ProjectContent::class, 'project_id', 'id');
}
public function contents(): HasMany
{
return $this->hasMany(ProjectContent::class, 'project_id', 'id');
}
And for the ProjectContent like down below i am using the scoupe so i can access the record with languages i want

//Relationship to Project
public function project(): BelongsTo
{
return $this->belongsTo(Project::class, 'project_id','id');
}

//Scopes
public function scopeLanguage($query, $language)
{
return $query->where('language', $language);
}

//Relationship to Project
public function project(): BelongsTo
{
return $this->belongsTo(Project::class, 'project_id','id');
}

//Scopes
public function scopeLanguage($query, $language)
{
return $query->where('language', $language);
}
this code works for me i use afterStateHydrate just to show the data and this code to update

protected function handleRecordUpdate (Model $record, array $data): Model
{
$contents = $data['contents'];
unset($data['contents']);
$record->update($data);
if ($contents) {
collect($contents)->each(fn($contentData, $language) => $record->contents()->language($language)->each(function ($content) use ($contentData) {
$content->update($contentData);
}));
}
return $record;
}

protected function handleRecordUpdate (Model $record, array $data): Model
{
$contents = $data['contents'];
unset($data['contents']);
$record->update($data);
if ($contents) {
collect($contents)->each(fn($contentData, $language) => $record->contents()->language($language)->each(function ($content) use ($contentData) {
$content->update($contentData);
}));
}
return $record;
}
Want results from more Discord servers?
Add your server