2014-04-07 189 views
1

缺少論據2 照亮\數據庫\雄辯\型號::的setAttribute(),稱爲 /vagrant/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 2335 and definedLaravel缺少參數2

上面我試圖上傳一個CSV文件。

控制器:

public function postUpload() 
{ 
    if (Input::hasFile('file')) 
    { 
     DB::transaction(function() 
     { 
      // Clear out what we have written 
      DB::table('wc_program')->delete(); 

      $csv = new CsvFile(Input::file('file')->getRealPath()); 

      // Get the csv headers and move to the next line (the start of actual data) 
      $columns = $csv->getHeader(); 
      $csv->next(); 

      // Loop through the rows creating/saving a record for each 
      while($csv->valid()) 
      { 
       $row = $csv->current(); 
       $pc = new Programmes(); 

       for($i=0; $i<count($columns); $i++) 
       { 
        $pc->$columns[$i] = $row[$i]; 
       } 

       $pc->save(); 
       $csv->next(); 
      } 
     }); 

    return Redirect::to('admin/programmes')->with('flash_success', 'Upload completed &amp; new data inserted.'); 
    } 

然後,我有一個模型,那只是像很基本的,沒有什麼重大的這樣:

class Programmes extends Eloquent { 

protected $guarded = array('id'); 
//public static $rules = array(); 

protected $table = 'wc_program_1'; 

public $timestamps = false; 
} 

和路線設置,如:

Route::get('admin/programmes/excelUpload','[email protected]'); 
Route::post('admin/programmes/doUpload', ['as' => 'admin.programmes.doUpload', 'uses' => '[email protected]']); 

回答

2

的代碼片段Illuminate\Database\Eloquent\Model::setAttribute()需要兩個參數,鍵和它的值。看起來你的價值在被分配到列時丟失了。

我建議你在

$pc->$columns[$i] = $row[$i]; 

近調試代碼,請更新您的問題,如果你發現一些有用的:)