Dynamically Pass lesson_category_id and Store Lessons with id and title in JSON Field in CRUD Form #1226
Replies: 3 comments 2 replies
-
To achieve the desired JSON structure for your quotes and dynamically pass the lesson_category_id, you'll need to make some modifications to your code. Here's how you can approach this:
[
'name' => 'lessons',
'type' => 'select2_from_ajax_multiple',
'label' => 'Lessons',
'entity' => 'lessons',
'attribute' => 'title',
'data_source' => url('api/lessons'),
'placeholder' => 'Select lessons',
'minimum_input_length' => 0,
'dependencies' => ['lessonCategory'],
'include_all_form_fields' => true,
]
Route::get('api/lessons', 'App\Http\Controllers\Admin\LessonCrudController@fetchLessons');
public function fetchLessons()
{
$lessonCategoryId = request('lessonCategory');
$term = request('q');
$results = Lesson::where('lesson_category_id', $lessonCategoryId)
->where('title', 'LIKE', '%'.$term.'%')
->paginate(10);
return $results;
}
public function setQuotesAttribute($value)
{
$quotes = json_decode($value, true);
foreach ($quotes as &$quote) {
if (isset($quote['lessons'])) {
$lessonIds = $quote['lessons'];
$quote['lessons'] = Lesson::whereIn('id', $lessonIds)
->get(['id', 'title'])
->toArray();
}
}
$this->attributes['quotes'] = json_encode($quotes);
} These changes should allow you to:
For the Remember to adjust the namespaces and paths according to your project structure. Backpack CRUD Fields Documentation provides more information on using Backpack CRUD How-To Documentation offers guidance on creating dependent fields. If you need further assistance or clarification, please let me know! Relevant sources: Github Issue (closed): [Feature] Ability to define any (nested) fields inside "table" field |
Beta Was this translation helpful? Give feedback.
-
$lessonCategoryId = request()->input('lessonCategory'); I tried this, but it didn’t work. |
Beta Was this translation helpful? Give feedback.
-
Due to not activity, I will close this issue, but please feel free to re-open or create a new one. Cheers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is my code for saving and updating quotes in a JSON field, but it is being saved as follows:
But I want it to be saved like this:
And if possible, I have statically passed the lesson_category_id here:
I want to pass the selected value for this field here. Thank you for the help!
Beta Was this translation helpful? Give feedback.
All reactions