F
Filamentā€¢11mo ago
pmkevinrudd

BelongsTo select save issue

Having an issue with a select input not saving as the correct name, caused by different relationship naming. On create the knowledge_base_category_id select input is correct, when updating it tries to save as category_id. Code breakdown below. I have two models KnowledgeBaseArticle KnowledgeBaseCategory KnowledgeBaseArticle
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class KnowledgeBaseArticle extends Model
{
use HasFactory;

protected $casts = [
'published_at' => 'datetime',
];

protected $guarded = [];

public function category()
{
return $this->belongsTo(KnowledgeBaseCategory::class, 'knowledge_base_category_id');
}

public function user()
{
return $this->belongsTo(User::class);
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;

class KnowledgeBaseArticle extends Model
{
use HasFactory;

protected $casts = [
'published_at' => 'datetime',
];

protected $guarded = [];

public function category()
{
return $this->belongsTo(KnowledgeBaseCategory::class, 'knowledge_base_category_id');
}

public function user()
{
return $this->belongsTo(User::class);
}
}
KnowledgeBaseCategory
class KnowledgeBaseCategory extends Model
{
use HasFactory;

protected $guarded = [];

public function articles()
{
return $this->hasMany(KnowledgeBaseArticle::class, 'knowledge_base_category_id');
}
}
class KnowledgeBaseCategory extends Model
{
use HasFactory;

protected $guarded = [];

public function articles()
{
return $this->hasMany(KnowledgeBaseArticle::class, 'knowledge_base_category_id');
}
}
I have the following resource for KnowledgeBaseArticle
public static function form(Form $form): Form
{
return $form
->columns(3)
->schema([
Forms\Components\Section::make('General')
->columnSpan(2)
->columns(2)
->schema([
Forms\Components\TextInput::make('title')
->label('Title')
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
})
->required(),
Forms\Components\TextInput::make('slug'),
Forms\Components\RichEditor::make('content')
->columnSpan(2)
->label('Content')
->required(),
]),
Forms\Components\Section::make('Details')
->columnSpan(1)
->schema([
Forms\Components\Select::make('knowledge_base_category_id')
->label('Category')
->relationship('category', 'title')
->preload()
->createOptionForm([
Forms\Components\TextInput::make('title')
->required(),
Forms\Components\Textarea::make('description'),
])
->searchable()
->required(),
Forms\Components\Select::make('user_id')
->label('Published By')
->searchable()
->relationship('user', 'name')
->nullable(),
Forms\Components\DatePicker::make('published_at')
->label('Publish Date')
->nullable(),
])
]);
}
public static function form(Form $form): Form
{
return $form
->columns(3)
->schema([
Forms\Components\Section::make('General')
->columnSpan(2)
->columns(2)
->schema([
Forms\Components\TextInput::make('title')
->label('Title')
->live(debounce: 500)
->afterStateUpdated(function (Get $get, Set $set, ?string $old, ?string $state) {
if (($get('slug') ?? '') !== Str::slug($old)) {
return;
}

$set('slug', Str::slug($state));
})
->required(),
Forms\Components\TextInput::make('slug'),
Forms\Components\RichEditor::make('content')
->columnSpan(2)
->label('Content')
->required(),
]),
Forms\Components\Section::make('Details')
->columnSpan(1)
->schema([
Forms\Components\Select::make('knowledge_base_category_id')
->label('Category')
->relationship('category', 'title')
->preload()
->createOptionForm([
Forms\Components\TextInput::make('title')
->required(),
Forms\Components\Textarea::make('description'),
])
->searchable()
->required(),
Forms\Components\Select::make('user_id')
->label('Published By')
->searchable()
->relationship('user', 'name')
->nullable(),
Forms\Components\DatePicker::make('published_at')
->label('Publish Date')
->nullable(),
])
]);
}
Solution:
ye no im dumb, thanks šŸ˜„
Jump to solution
9 Replies
pmkevinrudd
pmkevinruddOPā€¢11mo ago
I think whats happening is filament is failing to transform the relationship to the knowledge_base_category_id and is instead trying to use the relationship name and appending _id turning into category_id
pmkevinrudd
pmkevinruddOPā€¢11mo ago
but only in the update part of the code, not sure how to code dive that to see where the issue would be coming from
No description
pmkevinrudd
pmkevinruddOPā€¢11mo ago
this all works if I modify the relationship name to knowledgeBaseCategory so defs something going on there
Vp
Vpā€¢11mo ago
I think you need to learn from laravel relationship, here is the link https://laravel.com/docs/10.x/eloquent-relationships#one-to-one-defining-the-inverse-of-the-relationship
Vp
Vpā€¢11mo ago
Specially this line
No description
pmkevinrudd
pmkevinruddOPā€¢11mo ago
the relationship is fine i think? article has a category, category can have many articles you can override the fk with the second param of belongsTo
Vp
Vpā€¢11mo ago
You have KnowledgeBaseCategory model, not Category model.. yes.. do this
pmkevinrudd
pmkevinruddOPā€¢11mo ago
i already did wait šŸ¤¦ā€ā™€ļø yeah code i pasted was wrong, still the same issue tho
public function category()
{
return $this->belongsTo(KnowledgeBaseCategory::class, 'knowledge_base_category_id');
}
public function category()
{
return $this->belongsTo(KnowledgeBaseCategory::class, 'knowledge_base_category_id');
}
still attempts to insert into category_id was messing with stuff to see if I was high or something and forgot to re-add that code but still bugged. I'll setup a reproduction repo to sanity check myself šŸ˜‚
Solution
pmkevinrudd
pmkevinruddā€¢11mo ago
ye no im dumb, thanks šŸ˜„
Want results from more Discord servers?
Add your server