Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. public function postUpload ()
  2. {
  3. if ( Input::hasFile('file') )
  4. {
  5. DB::transaction(function()
  6. {
  7. // Clear out what we have written
  8. DB::table('wc_program')->delete();
  9.  
  10. $csv = new CsvFile(Input::file('file')->getRealPath());
  11.  
  12. // Get the csv headers and move to the next line (the start of actual data)
  13. $columns = $csv->getHeader();
  14. $csv->next();
  15.  
  16. // Loop through the rows creating / saving a record for each
  17. while( $csv->valid() )
  18. {
  19. $row = $csv->current();
  20. $pc = new Programmes();
  21.  
  22. for( $i=0; $i<count($columns); $i++ )
  23. {
  24. $pc->$columns[$i] = $row[$i];
  25. }
  26.  
  27. $pc->save();
  28. $csv->next();
  29. }
  30. });
  31.  
  32. return Redirect::to('admin/programmes')->with('flash_success', 'Upload completed & new data inserted.');
  33. }
  34.  
  35. class Programmes extends Eloquent {
  36.  
  37. protected $guarded = array('id');
  38. //public static $rules = array();
  39.  
  40. protected $table = 'wc_program_1';
  41.  
  42. public $timestamps = false;
  43. }
  44.  
  45. Route::get('admin/programmes/excelUpload','ProgrammesController@excelUpload');
  46. Route::post('admin/programmes/doUpload', ['as' => 'admin.programmes.doUpload', 'uses' => 'ProgrammesController@postUpload']);
  47.  
  48. $pc->$columns[$i] = $row[$i];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement