PHPackages                             jeffochoa/factory-stories - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jeffochoa/factory-stories

ActiveLibrary[Database &amp; ORM](/categories/database)

jeffochoa/factory-stories
=========================

Create model factory stories in Laravel 5.\*

1.1(8y ago)7860.5k↓23.9%7[2 PRs](https://github.com/jeffochoa/factory-stories/pulls)MITPHP

Since Jul 10Pushed 7y ago3 watchersCompare

[ Source](https://github.com/jeffochoa/factory-stories)[ Packagist](https://packagist.org/packages/jeffochoa/factory-stories)[ Docs](https://github.com/jeffochoa/factory-stories)[ RSS](/packages/jeffochoa-factory-stories/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (3)Used By (0)

Laravel Model Factory Stories
-----------------------------

[](#laravel-model-factory-stories)

This package allows you to create complex model factories in separate classes reusable among your tests classes.

inspired by the [Model Factories podcast](http://twentypercent.fm/model-factories) on [twentypercent.fm](http://twentypercent.fm)

Installing
----------

[](#installing)

```
$ composer require jeffochoa/factory-stories
```

If you are using Laravel 5.4 or lower, add the service provider to the app.php file:

```
FactoryStories\Providers\StoryFactoryServiceProvider::class
```

On Laravel 5.5+, the provider will be discovered automatically.

The problem
-----------

[](#the-problem)

Let's say you have to do some tests over articles created with certain conditions like:

```
// Active articles, from Active Users, with tags attached

```

You may end creating something like

```
$user = factory(User::class)->states('active');
$tags = factory(Taxonomy::class, 3)->states('tag')->create();
$article = factory(Article::class)->create([
    user_id => $user->id
]);
$article->tags()->attach($tags->pluck('id')->toArray());
```

... or something like that.

Of course, you can always extract this to its own method in a helper class, but sometimes you may want to have each of these kinds of "stories" in its own class, even more when you need to add some extra methods to generate more complex data.

Creating new Factory Stories
----------------------------

[](#creating-new-factory-stories)

After installing this package, you'll have access to a new artisan command

```
$ php artisan make:factory-story SomeClassName
```

After you run this command you should see the new file under the "database/" directory

```
