dongsuo0517
2017-02-04 10:33 阅读 34
已采纳

获取未定义的偏移量:laravel中为1

I am new to Laravel and was developing small application for my practise. I am doing job search functionality. This error giving me alot trouble and confuses me alot.

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);
}

I am not getting where am i doing wrong, so any suggestion from given snippet and any modification in the code

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    doubi5127 doubi5127 2017-02-04 10:39

    change this line:

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

    to

     $search_results[$i]['department_name_info'] = $department_name[0]['department_name'];
    
    点赞 评论 复制链接分享
  • douju7245 douju7245 2017-02-04 10:43
    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. 
    }
    
    点赞 评论 复制链接分享

相关推荐