2017 © Pedro Peláez
 

library eloquent-populator

Automatically populate Eloquent models

image

guidocella/eloquent-populator

Automatically populate Eloquent models

  • Thursday, February 22, 2018
  • by guidocella
  • Repository
  • 5 Watchers
  • 41 Stars
  • 3,387 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 17 Versions
  • 17 % Grown

The README.md

Eloquent Populator

This package provides default attributes for Laravel model factories by guessing the best Faker formatters from columns' names and types. For example, if a column is called first_name, it will use $faker->firstName(). If unable to guess by the column's name, it will guess the formatter by the column's type; for example, it will use $faker->text() for a VARCHAR column, or a Carbon instance for a TIMESTAMP. Models of BelongsTo relationships that have been defined are created as well. Furthermore, if you use the Multilingual package, for translatable attributes Populator will generate arrays with a different value for each configured locale., (*1)

Compared to packages that generate factories once, you generally don't have to update your factories as you change their table definitions and they will be very small., (*2)

Installation

Install the package with Composer:, (*3)

composer require --dev guidocella/eloquent-populator

Model factory integration

Call Populator::guessFormatters($this->modelName()) in your factories' definition methods to get an array with the guessed formatters. You may merge these with custom attributes whose guessed formatter isn't accurate., (*4)

use GuidoCella\EloquentPopulator\Populator;

...

public function definition(): array
{
    return [...Populator::guessFormatters($this->modelName()), ...[
        'avatar' => $this->faker->imageUrl()
    ]];
}

If you execute php artisan stub:publish, you can include the call to Populator in factory.stub so that php artisan make:factory will add it., (*5)

After guessing a model's formatters once, they are cached in a static property even across different tests., (*6)

Seeding

Before seeding your database you'll want to call setSeeding()., (*7)

public function run()
{
    Populator::setSeeding();

Its effect is that nullable columns will have a 50% chance of being set to null or to the guessed formatter., (*8)

Testing

If setSeeding() wasn't called nullable columns will always be set to their guessed formatter. The idea is to simplify tests such as this:, (*9)

public function testSubmitWithoutName() {
    $user = User::factory()->make(['name' => null]);
    // test that submitting a form with $user's attributes causes an error
}

public function testSubmitWithCorrectName() {
    $user = User::factory()->make();
    // no need to specify the correct formatter for name since it can't be null
    // test that submitting a form with $user's attributes is succesful
}

On the other hand, seeding the database with null attributes lets you notice Trying to get property of non-object errors., (*10)

The Versions

22/02 2018

dev-master

9999999-dev

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

08/02 2018

v2.1.3

2.1.3.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

30/08 2017

v2.1.2

2.1.2.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

12/07 2017

v2.1.1

2.1.1.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

20/06 2017

v2.1

2.1.0.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

29/05 2017

v2.0.3

2.0.3.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

13/05 2017

v2.0.2

2.0.2.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

18/04 2017

v2.0.1

2.0.1.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

26/03 2017

v2.0

2.0.0.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

18/02 2017

v1.2.2

1.2.2.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

13/02 2017

v1.2.1

1.2.1.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

03/02 2017

1.2

1.2.0.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

30/01 2017

v1.1.2

1.1.2.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

21/01 2017

v1.1.1

1.1.1.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

14/01 2017

v1.1.0

1.1.0.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

02/01 2017

v1.0.1

1.0.1.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker

16/12 2016

v1.0.0

1.0.0.0

Automatically populate Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent faker