响应:500 Internal Server Error,消息:“未定义偏移量:0”,异常:“ ErrorException”(在Collection.php中)

时间:2019-04-07 18:51:10

标签: php laravel eloquent laravel-5.6

基本上,我具有以下主题和级别的表结构,尽管很少有级别可以具有ID 1的父级,而很少有类别可以具有另一个父级ID。请参见以下示例

在这种情况下,学前,小学,中级是学术类别的级别

主题类别和级别

--------------------------------------------------
| Id | Name        | Parent_id       |
--------------------------------------------------
| 1 | Academic     | NULL            |
--------------------------------------------------
| 2 | Arts         | NULL            |
--------------------------------------------------
| 3 | Languages    | NULL            |
--------------------------------------------------
| 4 | Pre School   | 1               |
--------------------------------------------------
| 5 | Primary      | 1               |
--------------------------------------------------
| 6 | Intermediate | 1               |
--------------------------------------------------

除此之外,我还有一个与学术类别相关的学科表,请参见以下示例

主题

--------------------------------------------------
| Id | Name       | slug       |
--------------------------------------------------
| 1 | Drawing     | drawing    |
--------------------------------------------------
| 2 | English     | english    |
--------------------------------------------------
| 3 | Maths       | maths      |
--------------------------------------------------

主题类别关系

--------------------------------------------------
| Id | subject_id   | subject_category_id       
--------------------------------------------------
| 1 | 1             | 4       
--------------------------------------------------
| 2 | 2             | 5      
--------------------------------------------------
| 3 | 3             | 6      
--------------------------------------------------

当我尝试获取所有选中的please check the screenshot主题列表时出现以下错误

注意: 它的列表在加载时可以正常显示,但是它给了我表单提交和选择主题加载时的错误,尽管记录已正确保存在数据库中。

exception: "ErrorException"
file: "/[project-directory]/vendor/laravel/framework/src/Illuminate/Support/Collection.php"
line: 1802
message: "Undefined offset: 0"

请帮助我解决此问题

路线:

Route::resource('/tutor/subjects', 'TutorSubjectAPIController')->except([
        'create', 'edit', 'show', 'update'
    ]);

控制器(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);
        }
    }

主题映射器

public static function subjectsMapper($subjects){
        foreach ($subjects as $subject) {
            $data[] = [
                'subjectId' => $subject->pivot->subject_id,
                'categoryId' => $subject->pivot->subject_category_id,
            ];
        }
        return $data;
    }

1 个答案:

答案 0 :(得分:0)

问题是您在控制器中使用集合作为数组。 实际上,您正在尝试访问$findTutorParentCategory['0']->subject_category_id;,但是$findTutorParentCategory是查询在其上方的行中返回的集合。

如果查询仅返回一个结果,只需在查询中将->get()替换为->first()

要获取主题类别ID,您可以直接从 findTutorParentCategory 变量获取它,而无需使用is作为数组:

public function index(Request $request)
{
    // Rest of code..
        $findTutorParentCategory = DB::table('subject_tutor')->where([
            'tutor_id' => $user->tutor->id,
            'subject_id' => 0
        ])->first();
        $parentCategory =  $findTutorParentCategory->subject_category_id;
    // Rest of code..
}

更新:聊天之后,我们找到了解决方法

public function index(Request $request)
{
    $tutor = $request->user()->tutor;
    $tutor->load('subjects.categories');

    if ($tutor->subjects->isEmpty()) {
        return $this->sendResponse('', 'No subjects found.', 206);
    }

    $subjects = $tutor->subjects->map(function ($subject) { 
        $category = $subject->pivot->subject_category_id;

        return [
            'subject_id ' => $subject->pivot->subject_id,
            'category_id ' => $category,
            'parent_category_id' => $subject->categories()->find($category)->parent_id
        ]; 
    });

    // dd($subjects); 
    return $this->sendResponse($subjects); 
}

为每个与导师相关的科目生成subject_id, category_id, subject_category_id的元组。