2017-02-04 103 views
0

我是Laravel的新手,正在爲我的練習開發小型應用程序。我正在做求職功能。這個錯誤給了我很多麻煩,並且讓我很困惑。獲取未定義的抵消:1 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); 
} 

,我沒有得到我在哪裏做錯了,所以從給定片斷的任何建議,並在代碼

+1

可能重複[PHP:「注意:Undefined vari能夠「,」注意:未定義的索引「和」注意:未定義的偏移「](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef ) –

+0

請在運行for循環前請var_dump full $ search_results,然後在您的問題 –

回答

0

改變這一行任何修改:

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

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

oops中添加數組。明白了......謝謝德賽先生。 $ DEPARTMENT_NAME [0] [ '部門名稱']; [0]索引在獲取數據時是靜態的。 –

+0

不客氣。如果解決,然後接受答案 –

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. 
}