Skip to content

Commit

Permalink
Update README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
rymanalu committed Mar 24, 2017
1 parent ae5580f commit de57eb1
Showing 1 changed file with 70 additions and 2 deletions.
72 changes: 70 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,71 @@
# Laravel 5 Model Factory Generator [WIP]
# Laravel 5 Model Factory Generator

This package offer a lazy way to create a new model factory files, since Laravel has no Artisan command to generate it.
This package offers a lazy way to create a new model factory files, since Laravel has no Artisan command to generate it.

## Installation
First, install this package via the Composer package manager:
```
composer require rymanalu/factory-generator
```

Next, you should add the `FactoryGeneratorServiceProvider` to the `providers` array of your `config/app.php` configuration file:
```php
Rymanalu\FactoryGenerator\FactoryGeneratorServiceProvider::class,
```
Now, you should be able to generate a new model factory file by executing `php artisan make:factory` command.

## Usage
`php artisan make:factory` accept one argument: the model class name with the namespace. Make sure the model is already exists before execute this command.

Example:

```
php artisan make:factory "App\Post"
```
The command will generate a file named `PostFactory.php` in `path/to/your-laravel-project/database/factories` directory:

```php
<?php

use App\Post;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
//
});
```
This command also using the fillable array of the model and assign it to `$faker->word` as default (you can change it to the proper [Faker](https://github.com/fzaninotto/Faker) Formatters later) in the generated model factory.

For example, if the `App\Post` has fillable array like this:
```php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'text',
];
}
```
Then, the command will generate the `PostFactory.php` like this:
```php
<?php

use App\Post;
use Faker\Generator as Faker;

$factory->define(Post::class, function (Faker $faker) {
return [
'text' => $faker->word,
];
});
```

0 comments on commit de57eb1

Please sign in to comment.