piyook / csvseeder
Seed data into Laravel DB tables from .csv files
Requires
- php: >=8.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.5
README
CSV Database Seeder Utility Service
A simple Laravel utility package to seed database tables with CSV data.
Explore the docs ยป
About The Project
A simple utility for Laravel that allows a database to be quickly populated using data from a file in CSV format.
A custom Artisan make: command builds the CSV seeder class which then imports data from a CSV file stored in database/data folder.
Built With
- Laravel 9
Getting Started
The code can be added to a fresh Laravel install or an existing project.
Prerequisites
Laravel 8+
Installation
Install the package using composer
composer require --dev piyook/csvseeder
Usage
A custom Artisan make command was developed to make scaffolding a Seeder Class to import CSV files easier.
The CSV data file must share the same name as the table and be stored in a database/data folder.
To use the Artisan command simply type :
php artisan make:csvseeder <name> <columns> <path> (optional)
Where the table name to be seeded is passed to the make command along with an ARRAY of column names [].
Note that if the table name is plural then the singular form will be used for the class name
If the seeder class is to be stored in a location OTHER than database/seeders then you can specify this new path as the final option.
E.g A new Seeder Class for a table called "examples" which will import from the "examples.csv" file held in database/data, will be saved to the database/seeders directory with the filename "ExampleCSV.php".
php artisan make:csvseeder examples [col1,col2,col3,col4]
then
php artisan migrate:fresh php artisan db:seed --class=ExampleCSV
DONT FORGET TO REGISTER THE NEW CSV SEEDER IN THE DatabaseSeeder CLASS IN THE SEEDERS FOLDER BEFORE RUNNING A GENERAL DB SEED AS PART OF A MIGRATION WITH THE --SEED OPTION
Testing
Orchestra Testbench would normally be used to run unit and feature tests in the package but the seeder function would not work (the seeder class was not found).
A full seperate Laravel test project was set-up and the tests, phpunit.xml and the database/data, database/migration folders and files in the package test folder copied into the relevant places in the project to set up the test environment.
All Feature tests work running with Artisan test command.
Contact
Email: piyook@piyook.com
Project Link: [laravelcsvseed]