l5-fixtures
, (*1)
Fixtures package for Laravel 5 with support for JSON, CSV, YAML and PHP files., (*2)
If you are seeding your database with fake data that can be easily generated, consider using the Model Factories., (*3)
But if you need to load data that can't be generated then this is your best choice., (*4)
Installation
In order to install Laravel 5 Fixtures, just add, (*5)
"mayconbordin/l5-fixtures": "dev-master"
to your composer.json. Then run composer install
or composer update
., (*6)
Then in your config/app.php
add, (*7)
'Mayconbordin\L5Fixtures\FixturesServiceProvider'
in the providers
array and, (*8)
'Fixtures' => 'Mayconbordin\L5Fixtures\FixturesFacade'
to the aliases
array., (*9)
If you are using Laravel 5.5, package will be automatically discovered, no need to edit config/app.php
, (*10)
Configuration
To publish the configuration for this package execute php artisan vendor:publish
and a fixtures.php
file will be created in your app/config
directory., (*11)
Usage
By default the fixtures directory is /fixtures
, inside it you should place the data files that will fill
the database. The name of the file should be exactly the same as the name of the database table (e.g.: 'table_one.json'). Take a look at the two examples
in the /tests_data
directory., (*12)
To apply all fixtures to the database run, (*13)
Fixtures::up();
If you only want to apply some fixtures, you can pass an array with the name of the fixtures you want to apply, (*14)
Fixtures::up('table_one', 'table_two');
And to destroy the records in the database run, (*15)
Fixtures::down();
The down
method can also receive an array with the name of fixtures that will be destroyed. Currently all records
in the database tables are destroyed., (*16)
If you haven't published the configuration file or you want to load fixtures from another location, you only need to execute the following code before applying the fixtures:, (*17)
Fixtures::setUp('/path/to/fixtures');
The fixtures files are parsed in order to create an array of records that are themselves associative arrays. The resulting array is then inserted in the database using the insert method of the query builder., (*18)
Relations are not handled by the library, but you can make reference to other records by their IDs, even if they haven't been inserted yet because the library disables the foreign key checks before inserting the fixtures into the database., (*19)
JSON
[
{
"name": "Owen Sound",
"region": "ON",
"country": "Sierra Leone"
}
]
CSV
The delimiter is detected automatically., (*20)
name;region;country
Owen Sound;ON;Sierra Leone
YAML
- name: Owen Sound
region: ON
country: Sierra Leone
PHP
<?php
return [
[
'name' => 'Owen Sound',
'region' => 'ON',
'country' => 'Sierra Leone'
]
];