获取未定义的偏移量:laravel

时间:2017-02-04 10:33:04

标签: php laravel

我是Laravel的新手,正在为我的练习开发小应用程序。我正在做求职功能。这个错误给了我很多麻烦,让我很困惑。

public function job_search(Request $request) {
    $search_skill_set = $request->job_skills;
    $search_results = JobPost::whereRaw('FIND_IN_SET(?, job_skills)', $search_skill_set)
        ->get()
        ->toArray();

    for ($i = 0; $i < count($search_results); $i++) {
        $department_id = (int)$search_results[$i]['department_name'];
        $department_name = Department::select('department_name')
            ->where('id', '=', $department_id)
            ->get()
            ->toArray();

        // the next statement raises an Undefined:offset 1 error
        $search_results[$i]['department_name_info'] = $department_name[$i]['department_name'];
    }
    var_dump($search_results);
}

我没有得到我做错的地方,所以来自给定代码段的任何建议以及代码中的任何修改

2 个答案:

答案 0 :(得分:0)

更改此行:

 $search_results[$i]['department_name_info'] = $department_name[$i]['department_name'];

 $search_results[$i]['department_name_info'] = $department_name[0]['department_name'];

答案 1 :(得分:0)

for ($i=0; $i < count($search_results) ; $i++) { 
    $department_id = (int)$search_results[$i]['department_name'];

    //I am getting department id correct here
    $department_name = Department::select('department_name')->where('id','=',$department_id)->get()->toArray();
    //$depratment_name is also going okay and working

    $search_results[$i]['department_name_info'] = $department_name[0]['department_name'];
    // This line should have a static index. 
}