mayconbordin / l5-fixtures
A fixtures package for Laravel 5
Installs: 123 408
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 7
Open Issues: 1
Requires
- php: >=5.5.0
- illuminate/console: 5.x
- illuminate/database: 5.x
- illuminate/support: 5.x
- league/csv: ~8.0.0
- league/flysystem: ~1.0.16
- symfony/yaml: ~2.8.2
Requires (Dev)
- mockery/mockery: ~0.9.4
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-11-09 18:27:26 UTC
README
Fixtures package for Laravel 5 with support for JSON, CSV, YAML and PHP files.
If you are seeding your database with fake data that can be easily generated, consider using the Model Factories.
But if you need to load data that can't be generated then this is your best choice.
Installation
In order to install Laravel 5 Fixtures, just add
"mayconbordin/l5-fixtures": "dev-master"
to your composer.json. Then run composer install
or composer update
.
Then in your config/app.php
add
'Mayconbordin\L5Fixtures\FixturesServiceProvider'
in the providers
array and
'Fixtures' => 'Mayconbordin\L5Fixtures\FixturesFacade'
to the aliases
array.
If you are using Laravel 5.5, package will be automatically discovered, no need to edit config/app.php
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.
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.
To apply all fixtures to the database run
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
Fixtures::up('table_one', 'table_two');
And to destroy the records in the database run
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.
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:
Fixtures::setUp('/path/to/fixtures');
Data Format
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.
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.
JSON
[ { "name": "Owen Sound", "region": "ON", "country": "Sierra Leone" } ]
CSV
The delimiter is detected automatically.
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' ] ];