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);
    }
}

KnowledgeBaseCategory
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(),
                    ])
            ]);
    }
Solution
ye no im dumb, thanks 😄
Was this page helpful?