Package Data | |
---|---|
Maintainer Username: | Hesto |
Maintainer Contact: | jendrusha@gmail.com (Andrzej Żmudziński) |
Package Create Date: | 2017-02-17 |
Package Last Update: | 2019-12-17 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-12-11 15:24:02 |
Package Statistics | |
---|---|
Total Downloads: | 5,014 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Easy seeding database for Laravel Applications
composer require codingphase/seeder
Add your new provider to the providers
array of config/app.php
:
'providers' => [
// ...
CodingPhase\Seeder\SeederServiceProvider::class,
// ...
],
Publish config
php artisan vendor:publish --provider="CodingPhase\Seeder\SeederServiceProvider"
Extend your seeders with ModelSeeder:
use CodingPhase\Seeder\ModelSeeder;
class UsersTableSeeder extends ModelSeeder
{
...
}
Implement run method:
/**
* Run the database seeds.
*
* @return void
*/
public function run
{
//Example
$users = $this->seedModel(\App\User::class, function ($user) {
$user->save();
});
}
##API:
Default amount of seeding resources that are seeded are stored in config. If you want to seed another value of resources, you can.
$this->setAmount(30)->seedModel(\App\User::class, function ($user) {
$user->save();
});
Define header before progress bar in output (default is model namespace)
$this->setHeader("Awesome Users")->seedModel(\App\User::class, function ($user) {
$user->save();
});
Default true. Define style of Progress Bar.
$this->setAmount(30)->seedModel(\App\User::class, function ($user) {
$user->save();
});
Set data that will be used to fill resources. It overrides model factory data.
$data = [
1 => [
'name' => 'test',
'email' => 'test@test.com'
],
25 => [
'name' => 'example'
'email' => 'example@example.com',
],
];
$this->useData($data)->seedModel(\App\User::class, function ($user) {
$user->save();
});
test
and email test@test.com
example
and email example@example.com
##Practical Examples
use CodingPhase\Seeder\ModelSeeder;
class UsersTableSeeder extends ModelSeeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run
{
$adminsData = [
1 => [
'name' => 'test',
'email' => 'test@test.com',
'password' => bcrypt('123456')
],
4 => [
'name' => 'test4',
'email' => 'test4@test.com',
'password' => bcrypt('654321')
],
];
$admins = $this->useData($adminsData)
->setAmount(5)
->setHeader("Seeding Admins")
->setCompact(false)
->seedModel(\App\User::class, function ($user) {
$user->admin = 1;
$user->save();
});
$users = $this->setHeader("Seeding Regular Users")
->seedModel(\App\User::class, function ($user) {
$user->save();
});
}
}