To access the parent model within the default closure of a Filament form input, you can use the ->context() method to retrieve the parent model. Assuming that you are within the context of a child resource and you want to set the default value of chapter_number based on the parent course_id, you can do something like this:
TextInput::make('chapter_number')
->required()
->numeric()
->default(function ($context) {
// Access the parent model using $context
$parent = $context->record;
// Now you can use the parent model to set the default value
$lastLesson = Lesson::where('course_id', $parent->id)
->orderBy('lesson_number', 'DESC')
->first();
// If there is a last lesson, increment its number by 1, otherwise default to 1
return $lastLesson ? $lastLesson->lesson_number + 1 : 1;
}),
In this code snippet, $context->record will give you the parent model instance, which you can then use to query the Lesson model and find the last lesson number. Make sure to handle the case where there might not be any lessons yet, in which case you would want to default to 1.
Please note that the method orderBb in your original code seems to be a typo and should be orderBy. Also, ensure that the relationship between the parent and child models is correctly set up in your Eloquent models.