0

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

xmoex
  • 2,602
  • 22
  • 36
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – u_mulder Feb 04 '17 at 10:35
  • Please var_dump full $search_results before running for loop, and add the array in your question – Rana Ghosh Feb 04 '17 at 10:41

2 Answers2

0

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'];
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • oops. got it...thanks Desai sir. $department_name[0]['department_name']; The [0] index would be static for fetching the data. – Pratik Modak Feb 04 '17 at 10:41
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. 
}
Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20