Basically, I have the following table structure for subject and Levels, Although few levels can have the parent of id 1 and few categories can have the other parent id. please see the following example
In this case Pre School, Primary, Intermediate are the levels of the Academmics Category
Subject Categories & Levels
--------------------------------------------------
| Id | Name | Parent_id |
--------------------------------------------------
| 1 | Academic | NULL |
--------------------------------------------------
| 2 | Arts | NULL |
--------------------------------------------------
| 3 | Languages | NULL |
--------------------------------------------------
| 4 | Pre School | 1 |
--------------------------------------------------
| 5 | Primary | 1 |
--------------------------------------------------
| 6 | Intermediate | 1 |
--------------------------------------------------
Other than that, I have a subjects table, which is relevant to the academics category, please see the following example
Subjects
--------------------------------------------------
| Id | Name | slug |
--------------------------------------------------
| 1 | Drawing | drawing |
--------------------------------------------------
| 2 | English | english |
--------------------------------------------------
| 3 | Maths | maths |
--------------------------------------------------
Subject category relations
--------------------------------------------------
| Id | subject_id | subject_category_id
--------------------------------------------------
| 1 | 1 | 4
--------------------------------------------------
| 2 | 2 | 5
--------------------------------------------------
| 3 | 3 | 6
--------------------------------------------------
I am getting the following error when I am trying to get list of all the subjects with selected,
Note: Its listing fine on load, but Its giving me the error on form submission and loading with selected subjects, Although record is saving in the database correctly.
exception: "ErrorException"
file: "/[project-directory]/vendor/laravel/framework/src/Illuminate/Support/Collection.php"
line: 1802
message: "Undefined offset: 0"
Please help me to fix this
Route:
Route::resource('/tutor/subjects', 'TutorSubjectAPIController')->except([
'create', 'edit', 'show', 'update'
]);
Controller (API)
public function index(Request $request)
{
$user = $request->user();
$tutorSubjects = $user->tutor->subjects;
if(!$tutorSubjects->isEmpty()){
$tutorSubjectsData['data'] = self::subjectsMapper($tutorSubjects);
$findTutorParentCategory = DB::table('subject_tutor')->where([
'tutor_id' => $user->tutor->id,
'subject_id' => 0
])->get();
$parentCategory = $findTutorParentCategory['0']->subject_category_id;
$tutorSubjectsData['parent_category_id'] = $parentCategory;
return $this->sendResponse($tutorSubjectsData);
}else{
return $this->sendResponse('', 'No subjects found.',206);
}
}
Subject Mapper
public static function subjectsMapper($subjects){
foreach ($subjects as $subject) {
$data[] = [
'subjectId' => $subject->pivot->subject_id,
'categoryId' => $subject->pivot->subject_category_id,
];
}
return $data;
}