Store course_id in lesson_filters on Create/Update #1318
Replies: 2 comments 2 replies
-
To store the course_id in the lesson_filters table based on the selected course during creation or update, you need to use Laravel's model events. Since Backpack doesn't have callbacks, you can leverage Eloquent events to accomplish this. Here's how you can modify your code: protected function setupCreateOperation()
{
Widget::add()->type('script')->content('js/admin/lesson.js');
CRUD::setValidation(LessonRequest::class);
CRUD::field('title')->type('text');
CRUD::field('course')
->type('select2')
->entity('course');
CRUD::addField([
'name' => 'lessonFilter',
'label' => 'Lesson Filters',
'type' => "relationship",
'max_rows' => 1,
'subfields' => [
[
'name' => 'course_id',
'type' => 'hidden',
],
[
'name' => 'character_id',
'label' => 'Character',
'type' => 'relationship',
'model' => "App\Models\Character",
'ajax' => true,
'minimum_input_length' => 0,
'allows_null' => true,
'inline_create' => [
'entity' => 'character',
'force_select' => true,
],
'wrapper' => [
'class' => 'form-group col-md-4',
],
],
[
'name' => 'theme_id',
'label' => 'Theme',
'type' => 'relationship',
'model' => "App\Models\Theme",
'ajax' => true,
'minimum_input_length' => 0,
'allows_null' => true,
'inline_create' => [
'entity' => 'theme',
'force_select' => true,
],
'wrapper' => [
'class' => 'form-group col-md-4',
],
],
],
]);
// Use model events to set the course_id in lessonFilter
\App\Models\Lesson::saving(function($entry) {
// This will run before the lesson is saved
if (request()->has('course')) {
$entry->lessonFilter->course_id = request()->input('course');
}
});
} Alternatively, you can override the use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }
public function store()
{
// Do something before validation, before save, before everything
$response = $this->traitStore();
// Get the created entry
$lesson = $this->crud->entry;
// Update the lessonFilter with the course_id
if ($lesson->lessonFilter && request()->has('course')) {
$lesson->lessonFilter->course_id = request()->input('course');
$lesson->lessonFilter->save();
}
return $response;
}
public function update()
{
// Do something before validation, before save, before everything
$response = $this->traitUpdate();
// Get the updated entry
$lesson = $this->crud->entry;
// Update the lessonFilter with the course_id
if ($lesson->lessonFilter && request()->has('course')) {
$lesson->lessonFilter->course_id = request()->input('course');
$lesson->lessonFilter->save();
}
return $response;
} Backpack will take care of creating/updating/deleting the secondary model after the "Save" button is clicked on the form, as mentioned in the Backpack documentation. You just need to make sure the course_id is properly set. For more information on using model events and overriding store/update methods, check out the Create Operation documentation and Callbacks section. ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Fields|Manage related entries in the same form (create, update, delete) Part 2 |
Beta Was this translation helpful? Give feedback.
-
Hey @avinashtelsa Sorry, the suggestion by the AI bot is for another use case. It was wrong here. It seems your table structure is:
Here is what you should do:
You can see more practice examples of similar implementations in the demo with many other relationship types: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
protected function setupCreateOperation()
{
Widget::add()->type('script')->content('js/admin/lesson.js');
}
I have the following code and I want to store the course_id in the child lesson_filters table based on the course_id during creation or update.
Beta Was this translation helpful? Give feedback.
All reactions