Relationship "id", fillable and saveRelationships

Hi everyone,

There is probably something I misuderstand. I have a "Course" entity and a "CourseCategory" entity. My relationship is as simple as :
public function course_category(): BelongsTo
{
    return $this->belongsTo(CourseCategory::class);
}


In my CourseResource I have the following select :
Forms\Components\Select::make('course_category_id')
->relationship('course_category', 'title')
->searchable()
->required(),


And in my CreateCourse :
protected function handleRecordCreation(array $data): Model
    {
        return auth()->user()->courses()->create($data);
    }


But if I submit my form, I always get "Field 'course_category_id' doesn't have a default value". The issue disappears if I add "course_category_id" in the $fillable of Course, but I do not like that idea.

I'm seeing there is the "saveRelationships(); " who is called automatically, but I'm not sure to see how it works because if I add course_category_id in the $fillable, it is totally useless, and if not, I have the issue shown above.

Can someone help me understand what's the best way to deal with that ?
Thanks !
Was this page helpful?