triasrahman/jsonseeder

从 JSON 文件快速高效地填充 Laravel 数据库

v1.0.0 2015-09-05 16:39 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:21:24 UTC


README

Build Status Total Downloads Latest Stable Version License

这是一个通过从 JSON 文件快速填充数据库来提高您开发工作流程效率的包。

安装

使用 composer 安装此包

composer require triasrahman/json-seeder

用法

为每个要填充的表准备 JSON 文件,格式为 {table-name}.json,例如

countries.json

[ 
	{name: 'Afghanistan', code: 'AF'}, 
	{name: 'Åland Islands', code: 'AX'}, 
	{name: 'Albania', code: 'AL'}, 
	{name: 'Algeria', code: 'DZ'}, 
	{name: 'American Samoa', code: 'AS'}, 
	{name: 'AndorrA', code: 'AD'}, 
	{name: 'Angola', code: 'AO'}, 
	{name: 'Anguilla', code: 'AI'}, 
	{name: 'Antarctica', code: 'AQ'}, 
	{name: 'Antigua and Barbuda', code: 'AG'}, 
	{name: 'Argentina', code: 'AR'}, 
	{name: 'Armenia', code: 'AM'}, 
	{name: 'Aruba', code: 'AW'}, 
	{name: 'Australia', code: 'AU'}, 
	{name: 'Austria', code: 'AT'}, 
	{name: 'Azerbaijan', code: 'AZ'}, 
	{name: 'Bahamas', code: 'BS'},
	...
]

将它们保存到 storage/database,例如

/storage
	/database
		- users.json
		- cities.json
		- countries.json
		- products.json
		- posts.json

在您的 Seeder 类(位于 database/seeds/)中,添加 use Triasrahman\JSONSeeder\JSONSeeder 命名空间和 use JSONSeeder 特性。

<?php

use Illuminate\Database\Seeder;
use Triasrahman\JSONSeeder\JSONSeeder;

class YourTableSeeder extends Seeder
{
	use JSONSeeder;

	...

然后在 run() 方法中,只需调用 $this->JSONSeed($tableName, $className, [$realtions])。例如

CountriesTableSeeder.php

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->JSONSeed('countries', '\App\Country');
    }
}

注意:您也可以在 DatabaseSeeder.php 中运行此操作!

只需在控制台运行 php artisan db:seed,然后就可以了!有关更多数据库填充用法,请参阅 官方 Laravel 文档

复杂的 JSON 结构

我们确实了解 Laravel 有一个强大的 ORM 名称 Eloquent。我们希望这个包可以更容易地帮助您。是的,您可以在其中导入关系型模型!目前支持

  • 一对一
  • 一对多
  • 多对多
  • 多态(即将推出)

例如,当我们有一个博客网站时,我们想填充帖子表。您可以轻松设置如下 JSON 文件。 posts.json

[{
	"title": "Hello World!",
	"content": "This is my new blog.",
	"year": 2014,
	"categories": [
		{
			"name": "Hello",
			"description": "This is a category"
		},
		{
			"name": "World",
			"description": "This is a category"
		}
	],
	"author": {
		"name": "John Doe",
		"email": "johndoe@mail.com"
	},
	"pictures": [
		{
			"title": "Picture 1",
			"path": "uploads/picture-1.jpg"
		},
		{
			"title": "Picture 2",
			"path": "uploads/picture-2.jpg"
		}
	],
	"status": "published"
},

...

]

之后,您可以使用以下选项运行。

$this->JSONSeed('posts', '\App\Post', [

	// has many
	'picture' => [
	'table' => 'pictures',          // Table name (optional). If blank, it will pluralize from the name
	'class' => '\App\Picture',      // Define the related class
	'foreign_key' => 'post_id',     // Define the foreign key in related class
	'flush' => true,                // If true, it will flush the related table (optional, default: false)
	],
	
	// many to many
	'categories' => 
	'table' => 'categories',        // Table name (optional). If blank, it will pluralize from the name
	'class' => '\App\Category',     // Define the related class
	'flush' => true,                // If true, it will flush the related table (optional, default: false)
	'many_to_many' => true,         // If this relation is many to many, set it true
	],
	
	// belongs to
	'author' => [
	'table' => 'users',             // Table name (optional). If blank, it will pluralize from the name
	'class' => '\App\User',         // Define the related class
	'local_key' => 'user_id',       // Define the local key in related class
	'flush' => true,                // If true, it will flush the related table (optional, default: false)
	],
]);

很简单,对吧?现在就试试吧!

许可协议

Laravel JSON Seeder 在 MIT 许可协议 下发布。

版权所有 2015 Trias Nur Rahman