响应:500内部服务器错误,消息:“未定义的偏移量:0”,异常:“ErrorException”(在Collection.php中)[重复]

问题描述 投票:0回答:1

基本上,我有主题和级别的以下表格结构,虽然很少级别可以拥有id 1的父级,而少数类别可以拥有另一个父级id。请参阅以下示例

在这种情况下,Pre School,Primary,Intermediate是Academmics Category的级别

主题类别和级别

--------------------------------------------------
| 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;
    }
php laravel eloquent laravel-5.6
1个回答
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..
}

Update: After some messages in chat, we got to the solution

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的元组。

© www.soinside.com 2019 - 2024. All rights reserved.