styde/seeder
Composer 安装命令:
composer require styde/seeder
包简介
Laravel's improved seeders
README 文档
README
This package for Laravel allow seeding your database with faker data. It is an alternative to Model Factories of Laravel 5.1. With this package you can seed a model of your application and its related models too, using the package Faker.
Installation
To install through Composer:
- Add the following instruction to the "require" object in your composer.json:
"styde/seeder": "^1.0"
or simply execute on your console:
composer require styde/seeder
Then run composer update.
- After Styde Seeder is installed, you need to add the service provider to the
providersarray inconfig/app.php
'providers' => [
// ...
Styde\Seeder\SeederServiceProvider::class,
// ...
],
- Then add the following to your
database/seeds/DatabaseSeeder.php:
<?php
use Styde\Seeder\BaseSeeder;
class DatabaseSeeder extends BaseSeeder
{
protected $truncate = array(
//'users',
);
protected $seeders = array(
//'User',
);
}
Specify the tables of database you want to $truncate (order does not matter since the foreign key check will be disabled) Then add the $seeders, by default it will autocomplete the suffix "TableSeeder" so no need to add it.
Usage
To create a new seeder file you can run:
php artisan styde:seeder NameOfSeeder
And a new file called NameOfSeederTableSeeder.php will be created at database/seeds directory.
Then complete your seeder with new instance of the Model in the getModel() method and its attributes in the getDummyData method. You can use Faker for generates fake data, for example:
php artisan styde:seeder User
Completing the Model in getModel() and some attributes with faker data:
<?php
use Styde\Seeder\Seeder;
use Faker\Generator;
use App\User;
class UserTableSeeder extends Seeder
{
protected $total = 50;
public function getModel()
{
return new User();
}
public function getDummyData(Generator $faker, array $customValues = array())
{
return [
'name' => $faker->name,
'email' => $faker->email,
'password' => bcrypt('secret'),
];
}
}
Once you run the seed command in Laravel php artisan db:seed it will create 50 users with random data by default.
Helpers
Also, you can use these two helpers when you are working with tests or php artisan tinker:
/**
* Create one instance or a collection of the given model and persist them to the database.
*
* @param string $seeder
* @param integer $total
* @param array $customValues
*
* @return mixed
*/
function seed($seeder, $total = 1, array $customValues = array())
For example to create 5 users seed('User', 5) or to create one user with specific data seed('User', ['name' => 'John', 'email' => 'john@example.com']).
/**
* Create one instance or a collection of a given model.
*
* @param string $seeder
* @param integer $total
* @param array $customValues
*
* @return mixed
*/
function model($seeder, $total = 1, array $customValues = array())
With this helper you only can create an instance or collection, but will not be persisted to the database.
About
Styde Seeder was created by Duilio Palacios as part of the code for the course Crea tu primera aplicación con Laravel 5 (in Spanish)
styde/seeder 适用场景与选型建议
styde/seeder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.11k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2015 年 06 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「seeder」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 styde/seeder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 styde/seeder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 styde/seeder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Philippines region, province, cities/municipalities and barangays Laravel migration and table seeder.
Versioned, environment-based Seeders in Laravel. Used while original does not update to Laravel 5.7
Laravel package providing complete Algerian geo data (69 Wilayas and Communes) with slugs, coordinates and optimized seeders.
Create and use JSON files to seed your database in your Laravel applications
Alfabank REST API integration
CSV / XLS / ODS dummy data generator.
统计信息
- 总下载量: 6.11k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-14