Importing a CSV using Eloquent
When working with data, there are many different sources and file types that we may encounter. A common type is a CSV, or comma separated value, file. In this recipe, we'll take a CSV file's contents and insert them into our database.
Getting ready
To get started, we need to have a standard Laravel installation that's configured with a MySQL database. We also need to have our migrations table created by running the Artisan command, php artisan migrate:install
.
How to do it...
To complete this recipe, follow these steps:
In a text editor, create a file named
scifi.csv
, save it to your application'spublic
folder. Add in the following data:Spock,Star Trek Kirk,Star Trek Luke,Star Wars Lando,Star Wars Deckard,Blade Runner Dave,2001
In the command prompt, create a migration:
php artisan migrate:make create_scifi_table
Open the migration file that was just created and add in our schema:
use Illuminate\Database\Migrations\Migration; class CreateScifiTable extends Migration...