Last active
September 29, 2022 10:27
-
-
Save t0rik/3f887ef74ca0a107857f35d6c64be1f6 to your computer and use it in GitHub Desktop.
Import csv file to laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Seeders; | |
use Illuminate\Database\Seeder; | |
use App\Models\Transaction; | |
class TransactionSeeder extends Seeder | |
{ | |
/** | |
* Run the database seeds. | |
* | |
* @return void | |
*/ | |
public function run() | |
{ | |
Transaction::truncate(); | |
$csvData = fopen(base_path('database/csv/transaction_report.csv'), 'r'); | |
$transRow = true; | |
while (($data = fgetcsv($csvData, 555, ',')) !== false) { | |
if (!$transRow) { | |
Transaction::create([ | |
'transaction_date' => $data['0'], | |
'price' => $data['1'], | |
'payment_type' => $data['2'], | |
'name' => $data['3'], | |
'city' => $data['4'], | |
'us_zip' => $data['5'], | |
]); | |
} | |
$transRow = false; | |
} | |
fclose($csvData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment