Advertisement
chriz74

seed from csv using create for timestamps

May 21st, 2015
1,569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. <?php
  2.  
  3. class ModelsTableSeeder extends Seeder {
  4.  
  5.     public function run()
  6.     {
  7.         Eloquent::unguard();
  8.         // Uncomment the below to wipe the table clean before populating
  9.         //DB::table('models')->delete();
  10.        
  11.         // Uncomment the below if you need to reset the id autoincrement counter
  12.         //DB::statement('ALTER TABLE models AUTO_INCREMENT=1');
  13.  
  14.         function csv_to_array($filename='', $delimiter=';')
  15.             {
  16.                 if(!file_exists($filename) || !is_readable($filename))
  17.                     return FALSE;
  18.                
  19.                 $header = NULL;
  20.                 $data = array();
  21.                 if (($handle = fopen($filename, 'r')) !== FALSE)
  22.                 {
  23.                     while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
  24.                     {                                          
  25.                         if(!$header)
  26.                             $header = $row;
  27.  
  28.                         else
  29.                             $data[] = array_combine($header, $row);                    
  30.                                                                                
  31.                     }
  32.                     fclose($handle);
  33.                 }
  34.                 return $data;
  35.                
  36.             }
  37.  
  38.             /****************************************
  39.             * CSV FILE SAMPLE *
  40.             ****************************************/
  41.             // id,subdireccion_id,idInterno,area
  42.             // ,1,4,AREA MALAGA OCC
  43.             // ,1,2,AREA MALAGA N/ORIENT
  44.              
  45.             $csvFile = public_path().'/models.csv';
  46.  
  47.             $models = csv_to_array($csvFile);
  48.  
  49.             // Uncomment the below lines to run the seeder
  50.             // foreach ($models as $model)
  51.             //  {
  52.                 //  Models::create($model);
  53.             //  }                                                                      
  54.                    
  55.            
  56.  
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement