数据填充

This commit is contained in:
fthvgb1 2018-01-01 12:38:52 +08:00
parent 3c525feee1
commit b8973687ca
9 changed files with 623 additions and 11 deletions

View File

@ -24,6 +24,8 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register() public function register()
{ {
// if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
} }
} }

View File

@ -9,6 +9,7 @@
"type": "project", "type": "project",
"require": { "require": {
"php": ">=7.0.0", "php": ">=7.0.0",
"doctrine/dbal": "^2.6",
"fideloper/proxy": "~3.3", "fideloper/proxy": "~3.3",
"intervention/image": "^2.4", "intervention/image": "^2.4",
"laravel/framework": "5.5.*", "laravel/framework": "5.5.*",
@ -17,6 +18,7 @@
"overtrue/laravel-lang": "^3.0" "overtrue/laravel-lang": "^3.0"
}, },
"require-dev": { "require-dev": {
"barryvdh/laravel-ide-helper": "^2.4",
"filp/whoops": "~2.0", "filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4", "fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0", "mockery/mockery": "~1.0",

535
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "b9cc871ccce62e9980f1fe5316207e6e", "content-hash": "f7f5fa291c48b559f95e91f3fa42fa80",
"packages": [ "packages": [
{ {
"name": "caouecs/laravel-lang", "name": "caouecs/laravel-lang",
@ -81,6 +81,361 @@
"description": "implementation of xdg base directory specification for php", "description": "implementation of xdg base directory specification for php",
"time": "2014-10-24T07:27:01+00:00" "time": "2014-10-24T07:27:01+00:00"
}, },
{
"name": "doctrine/annotations",
"version": "v1.6.0",
"source": {
"type": "git",
"url": "https://github.com/doctrine/annotations.git",
"reference": "c7f2050c68a9ab0bdb0f98567ec08d80ea7d24d5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/annotations/zipball/c7f2050c68a9ab0bdb0f98567ec08d80ea7d24d5",
"reference": "c7f2050c68a9ab0bdb0f98567ec08d80ea7d24d5",
"shasum": ""
},
"require": {
"doctrine/lexer": "1.*",
"php": "^7.1"
},
"require-dev": {
"doctrine/cache": "1.*",
"phpunit/phpunit": "^6.4"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.6.x-dev"
}
},
"autoload": {
"psr-4": {
"Doctrine\\Common\\Annotations\\": "lib/Doctrine/Common/Annotations"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Roman Borschel",
"email": "roman@code-factory.org"
},
{
"name": "Benjamin Eberlei",
"email": "kontakt@beberlei.de"
},
{
"name": "Guilherme Blanco",
"email": "guilhermeblanco@gmail.com"
},
{
"name": "Jonathan Wage",
"email": "jonwage@gmail.com"
},
{
"name": "Johannes Schmitt",
"email": "schmittjoh@gmail.com"
}
],
"description": "Docblock Annotations Parser",
"homepage": "http://www.doctrine-project.org",
"keywords": [
"annotations",
"docblock",
"parser"
],
"time": "2017-12-06T07:11:42+00:00"
},
{
"name": "doctrine/cache",
"version": "v1.7.1",
"source": {
"type": "git",
"url": "https://github.com/doctrine/cache.git",
"reference": "b3217d58609e9c8e661cd41357a54d926c4a2a1a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/cache/zipball/b3217d58609e9c8e661cd41357a54d926c4a2a1a",
"reference": "b3217d58609e9c8e661cd41357a54d926c4a2a1a",
"shasum": ""
},
"require": {
"php": "~7.1"
},
"conflict": {
"doctrine/common": ">2.2,<2.4"
},
"require-dev": {
"alcaeus/mongo-php-adapter": "^1.1",
"mongodb/mongodb": "^1.1",
"phpunit/phpunit": "^5.7",
"predis/predis": "~1.0"
},
"suggest": {
"alcaeus/mongo-php-adapter": "Required to use legacy MongoDB driver"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.7.x-dev"
}
},
"autoload": {
"psr-4": {
"Doctrine\\Common\\Cache\\": "lib/Doctrine/Common/Cache"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Roman Borschel",
"email": "roman@code-factory.org"
},
{
"name": "Benjamin Eberlei",
"email": "kontakt@beberlei.de"
},
{
"name": "Guilherme Blanco",
"email": "guilhermeblanco@gmail.com"
},
{
"name": "Jonathan Wage",
"email": "jonwage@gmail.com"
},
{
"name": "Johannes Schmitt",
"email": "schmittjoh@gmail.com"
}
],
"description": "Caching library offering an object-oriented API for many cache backends",
"homepage": "http://www.doctrine-project.org",
"keywords": [
"cache",
"caching"
],
"time": "2017-08-25T07:02:50+00:00"
},
{
"name": "doctrine/collections",
"version": "v1.5.0",
"source": {
"type": "git",
"url": "https://github.com/doctrine/collections.git",
"reference": "a01ee38fcd999f34d9bfbcee59dbda5105449cbf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/collections/zipball/a01ee38fcd999f34d9bfbcee59dbda5105449cbf",
"reference": "a01ee38fcd999f34d9bfbcee59dbda5105449cbf",
"shasum": ""
},
"require": {
"php": "^7.1"
},
"require-dev": {
"doctrine/coding-standard": "~0.1@dev",
"phpunit/phpunit": "^5.7"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.3.x-dev"
}
},
"autoload": {
"psr-0": {
"Doctrine\\Common\\Collections\\": "lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Roman Borschel",
"email": "roman@code-factory.org"
},
{
"name": "Benjamin Eberlei",
"email": "kontakt@beberlei.de"
},
{
"name": "Guilherme Blanco",
"email": "guilhermeblanco@gmail.com"
},
{
"name": "Jonathan Wage",
"email": "jonwage@gmail.com"
},
{
"name": "Johannes Schmitt",
"email": "schmittjoh@gmail.com"
}
],
"description": "Collections Abstraction library",
"homepage": "http://www.doctrine-project.org",
"keywords": [
"array",
"collections",
"iterator"
],
"time": "2017-07-22T10:37:32+00:00"
},
{
"name": "doctrine/common",
"version": "v2.8.1",
"source": {
"type": "git",
"url": "https://github.com/doctrine/common.git",
"reference": "f68c297ce6455e8fd794aa8ffaf9fa458f6ade66"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/common/zipball/f68c297ce6455e8fd794aa8ffaf9fa458f6ade66",
"reference": "f68c297ce6455e8fd794aa8ffaf9fa458f6ade66",
"shasum": ""
},
"require": {
"doctrine/annotations": "1.*",
"doctrine/cache": "1.*",
"doctrine/collections": "1.*",
"doctrine/inflector": "1.*",
"doctrine/lexer": "1.*",
"php": "~7.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.8.x-dev"
}
},
"autoload": {
"psr-4": {
"Doctrine\\Common\\": "lib/Doctrine/Common"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Roman Borschel",
"email": "roman@code-factory.org"
},
{
"name": "Benjamin Eberlei",
"email": "kontakt@beberlei.de"
},
{
"name": "Guilherme Blanco",
"email": "guilhermeblanco@gmail.com"
},
{
"name": "Jonathan Wage",
"email": "jonwage@gmail.com"
},
{
"name": "Johannes Schmitt",
"email": "schmittjoh@gmail.com"
}
],
"description": "Common Library for Doctrine projects",
"homepage": "http://www.doctrine-project.org",
"keywords": [
"annotations",
"collections",
"eventmanager",
"persistence",
"spl"
],
"time": "2017-08-31T08:43:38+00:00"
},
{
"name": "doctrine/dbal",
"version": "v2.6.3",
"source": {
"type": "git",
"url": "https://github.com/doctrine/dbal.git",
"reference": "e3eed9b1facbb0ced3a0995244843a189e7d1b13"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/doctrine/dbal/zipball/e3eed9b1facbb0ced3a0995244843a189e7d1b13",
"reference": "e3eed9b1facbb0ced3a0995244843a189e7d1b13",
"shasum": ""
},
"require": {
"doctrine/common": "^2.7.1",
"ext-pdo": "*",
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^5.4.6",
"phpunit/phpunit-mock-objects": "!=3.2.4,!=3.2.5",
"symfony/console": "2.*||^3.0"
},
"suggest": {
"symfony/console": "For helpful console commands such as SQL execution and import of files."
},
"bin": [
"bin/doctrine-dbal"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.6.x-dev"
}
},
"autoload": {
"psr-0": {
"Doctrine\\DBAL\\": "lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Roman Borschel",
"email": "roman@code-factory.org"
},
{
"name": "Benjamin Eberlei",
"email": "kontakt@beberlei.de"
},
{
"name": "Guilherme Blanco",
"email": "guilhermeblanco@gmail.com"
},
{
"name": "Jonathan Wage",
"email": "jonwage@gmail.com"
}
],
"description": "Database Abstraction Layer",
"homepage": "http://www.doctrine-project.org",
"keywords": [
"database",
"dbal",
"persistence",
"queryobject"
],
"time": "2017-11-19T13:38:54+00:00"
},
{ {
"name": "doctrine/inflector", "name": "doctrine/inflector",
"version": "v1.2.0", "version": "v1.2.0",
@ -2571,6 +2926,128 @@
} }
], ],
"packages-dev": [ "packages-dev": [
{
"name": "barryvdh/laravel-ide-helper",
"version": "v2.4.1",
"source": {
"type": "git",
"url": "https://github.com/barryvdh/laravel-ide-helper.git",
"reference": "2b1273c45e2f8df7a625563e2283a17c14f02ae8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/barryvdh/laravel-ide-helper/zipball/2b1273c45e2f8df7a625563e2283a17c14f02ae8",
"reference": "2b1273c45e2f8df7a625563e2283a17c14f02ae8",
"shasum": ""
},
"require": {
"barryvdh/reflection-docblock": "^2.0.4",
"illuminate/console": "^5.0,<5.6",
"illuminate/filesystem": "^5.0,<5.6",
"illuminate/support": "^5.0,<5.6",
"php": ">=5.4.0",
"symfony/class-loader": "^2.3|^3.0"
},
"require-dev": {
"doctrine/dbal": "~2.3",
"illuminate/config": "^5.0,<5.6",
"illuminate/view": "^5.0,<5.6",
"phpunit/phpunit": "4.*",
"scrutinizer/ocular": "~1.1",
"squizlabs/php_codesniffer": "~2.3"
},
"suggest": {
"doctrine/dbal": "Load information from the database about models for phpdocs (~2.3)"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.3-dev"
},
"laravel": {
"providers": [
"Barryvdh\\LaravelIdeHelper\\IdeHelperServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Barryvdh\\LaravelIdeHelper\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Barry vd. Heuvel",
"email": "barryvdh@gmail.com"
}
],
"description": "Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.",
"keywords": [
"autocomplete",
"codeintel",
"helper",
"ide",
"laravel",
"netbeans",
"phpdoc",
"phpstorm",
"sublime"
],
"time": "2017-07-16T00:24:12+00:00"
},
{
"name": "barryvdh/reflection-docblock",
"version": "v2.0.4",
"source": {
"type": "git",
"url": "https://github.com/barryvdh/ReflectionDocBlock.git",
"reference": "3dcbd98b5d9384a5357266efba8fd29884458e5c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/barryvdh/ReflectionDocBlock/zipball/3dcbd98b5d9384a5357266efba8fd29884458e5c",
"reference": "3dcbd98b5d9384a5357266efba8fd29884458e5c",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"phpunit/phpunit": "~4.0,<4.5"
},
"suggest": {
"dflydev/markdown": "~1.0",
"erusev/parsedown": "~1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-0": {
"Barryvdh": [
"src/"
]
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Mike van Riel",
"email": "mike.vanriel@naenius.com"
}
],
"time": "2016-06-13T19:28:20+00:00"
},
{ {
"name": "doctrine/instantiator", "name": "doctrine/instantiator",
"version": "1.1.0", "version": "1.1.0",
@ -4219,6 +4696,62 @@
], ],
"time": "2017-11-09T06:53:14+00:00" "time": "2017-11-09T06:53:14+00:00"
}, },
{
"name": "symfony/class-loader",
"version": "v3.4.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/class-loader.git",
"reference": "e8d36a7b5568d232f5c3f8ef92665836b9f1e038"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/class-loader/zipball/e8d36a7b5568d232f5c3f8ef92665836b9f1e038",
"reference": "e8d36a7b5568d232f5c3f8ef92665836b9f1e038",
"shasum": ""
},
"require": {
"php": "^5.5.9|>=7.0.8"
},
"require-dev": {
"symfony/finder": "~2.8|~3.0|~4.0",
"symfony/polyfill-apcu": "~1.1"
},
"suggest": {
"symfony/polyfill-apcu": "For using ApcClassLoader on HHVM"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.4-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\ClassLoader\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony ClassLoader Component",
"homepage": "https://symfony.com",
"time": "2017-11-05T16:10:10+00:00"
},
{ {
"name": "theseer/tokenizer", "name": "theseer/tokenizer",
"version": "1.1.0", "version": "1.1.0",

View File

@ -3,7 +3,19 @@
use Faker\Generator as Faker; use Faker\Generator as Faker;
$factory->define(App\Models\Topic::class, function (Faker $faker) { $factory->define(App\Models\Topic::class, function (Faker $faker) {
//随机生成『小段落』文本
$sentence = $faker->sentence();
// 随机取一个月以内的时间
$updated_at = $faker->dateTimeThisMonth();
// 传参为生成最大时间不超过,创建时间永远比更改时间要早
$created_at = $faker->dateTimeThisMonth($updated_at);
return [ return [
// 'name' => $faker->name, 'title' => $sentence,
'body' => $faker->text(),
'excerpt' => $sentence,
'created_at' => $created_at,
'updated_at' => $updated_at,
]; ];
}); });

View File

@ -15,11 +15,14 @@ use Faker\Generator as Faker;
$factory->define(App\Models\User::class, function (Faker $faker) { $factory->define(App\Models\User::class, function (Faker $faker) {
static $password; static $password;
$now = \Carbon\Carbon::now()->toDateTimeString();
return [ return [
'name' => $faker->name, 'name' => $faker->name,
'email' => $faker->unique()->safeEmail, 'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'), 'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10), 'remember_token' => str_random(10),
'created_at' => $now,
'updated_at' => $now
]; ];
}); });

View File

@ -11,7 +11,7 @@ class DatabaseSeeder extends Seeder
*/ */
public function run() public function run()
{ {
// $this->call(UsersTableSeeder::class); $this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class); $this->call(TopicsTableSeeder::class);
} }
} }

View File

@ -7,12 +7,28 @@ class TopicsTableSeeder extends Seeder
{ {
public function run() public function run()
{ {
$topics = factory(Topic::class)->times(50)->make()->each(function ($topic, $index) { // 所有用户 ID 数组,如:[1,2,3,4]
if ($index == 0) { $user_ids = \App\Models\User::all()->pluck('id')->toArray();
// $topic->field = 'value';
}
});
// 所有分类 ID 数组,如:[1,2,3,4]
$category_ids = \App\Models\Category::all()->pluck('id')->toArray();
// 获取 Faker 实例
$faker = app(Faker\Generator::class);
$topics = factory(Topic::class)
->times(100)
->make()
->each(function ($topic, $index)
use ($user_ids, $category_ids, $faker) {
// 从用户 ID 数组中随机取出一个并赋值
$topic->user_id = $faker->randomElement($user_ids);
// 话题分类,同上
$topic->category_id = $faker->randomElement($category_ids);
});
// 将数据集合转换为数组,并插入到数据库中
Topic::insert($topics->toArray()); Topic::insert($topics->toArray());
} }

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// 获取 Faker 实例
$faker = app(Faker\Generator::class);
$avatars = [
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/s5ehp11z6s.png?imageView2/1/w/200/h/200',
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/Lhd1SHqu86.png?imageView2/1/w/200/h/200',
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/LOnMrqbHJn.png?imageView2/1/w/200/h/200',
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/xAuDMxteQy.png?imageView2/1/w/200/h/200',
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/ZqM7iaP4CR.png?imageView2/1/w/200/h/200',
'https://fsdhubcdn.phphub.org/uploads/images/201710/14/1/NDnzMutoxX.png?imageView2/1/w/200/h/200',
];
// 生成数据集合
$users = factory(\App\Models\User::class)
->times(10)
->make()
->each(function ($user, $index) use ($faker, $avatars) {
// 从头像数组中随机取出一个并赋值
$user->avatar = $faker->randomElement($avatars);
});
// 让隐藏字段可见,并将数据集合转换为数组
$user_array = $users->makeVisible(['password', 'remember_token'])->toArray();
// 插入到数据库中
\App\Models\User::insert($user_array);
// 单独处理第一个用户的数据
$user = \App\Models\User::find(1);
$user->name = 'fthvgb1';
$user->email = 'fthvgb1@163.com';
$user->avatar = '';
$user->save();
}
}

View File

@ -11,8 +11,6 @@
| |
*/ */
use Illuminate\Support\Facades\Route;
Route::get('/', 'PagesController@root')->name('home'); Route::get('/', 'PagesController@root')->name('home');
Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']]) Route::resource('users', 'UsersController', ['only' => ['update', 'show', 'edit']])