charles / jsonseeder
Quick and efficient seeding database from JSON files for Laravel
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: >=5.4.0
- illuminate/support: 5.*
This package is not auto-updated.
Last update: 2020-07-30 10:56:42 UTC
README
This is a package to make your development workflow efficient by seeding the database from JSON files quickly.
安装
使用composer安装:
composer require charles/jsonseeder
使用
为每个表按照 {table-name}.json
格式准备好JSON文件,例如:
countries.json
[
{"province":"北京市","cities":"北京市"},
{"province":"上海市","cities":"上海市"},
{"province":"天津市","cities":"天津市"},
{"province":"重庆市","cities":"重庆市"},
{"province":"安徽省","cities":"安庆市"},
{"province":"福建省","cities":"福州市"}
....
]
保存到 database/seeds
, 例如:
/database
/seeds
- users.json
- cities.json
- countries.json
- products.json
- posts.json
在 Seeder class 中(路径 database/seeds/
), 添加 use Charles\Seeds\JSONSeeder
命名空间,并且调用 use JSONSeeder
trait代码复用即可.
<?php
use Illuminate\Database\Seeder;
use Charles\Seeds\JSONSeeder;
class YourTableSeeder extends Seeder
{
use JSONSeeder;
...
在 run()
方法中直接使用 $this->JSONSeed($tableName)
. 例如:
CountriesTableSeeder.php
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->JSONSeed('countries');
}
}
NOTE: 也可以直接运行 DatabaseSeeder.php
!
在终端直接运行 php artisan db:seed
您也可以访问 Official Laravel Documentation 查看更多database使用说明
尝试一下会发现很简单吧!
License
Laravel JSON Seeder is licensed under the MIT License. Copyright 2019 [Charles Wang]