PHPackages                             thettler/laravel-factory-classes - 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. [Testing &amp; Quality](/categories/testing)
4. /
5. thettler/laravel-factory-classes

AbandonedArchivedLibrary[Testing &amp; Quality](/categories/testing)

thettler/laravel-factory-classes
================================

A Package to easy create Factory Classes for your tests with a fluent api and automatic auto completion

v0.0.2(6y ago)163961[2 PRs](https://github.com/thettler/laravel-factory-classes/pulls)MITPHPPHP ^7.4

Since Mar 12Pushed 4y ago1 watchersCompare

[ Source](https://github.com/thettler/laravel-factory-classes)[ Packagist](https://packagist.org/packages/thettler/laravel-factory-classes)[ Docs](https://github.com/christophrumpel/laravel-factories-reloaded)[ RSS](/packages/thettler-laravel-factory-classes/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (7)Versions (6)Used By (0)

Welcome to Laravel Factory Classes 👋
====================================

[](#welcome-to-laravel-factory-classes-)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b7045ea9f6d8c99db86cb457b80a7dc2ebb72e58dcd2392a61571939f5babb3e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f74686574746c65722f6c61726176656c2d666163746f72792d636c61737365732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thettler/laravel-factory-classes)[![Packagist](https://camo.githubusercontent.com/fb7b253b43795bd8330fe41fc52e02b979cac388d91cb43ba46a3a46cc200601/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f74686574746c65722f6c61726176656c2d666163746f72792d636c61737365733f7374796c653d666c61742d737175617265)](https://choosealicense.com/licenses/mit/)[![Total Downloads](https://camo.githubusercontent.com/deb6379c043a2e5181ef4de9e7729e04193466d847c21179b8c36da89f4b81a7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f74686574746c65722f6c61726176656c2d666163746f72792d636c61737365732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/thettler/laravel-factory-classes)[![Twitter: TobiSlice](https://camo.githubusercontent.com/9d99db73dd15064c4c4f60ffb0802d1e967c67b3fa13f32942cef4149fe9e6b8/68747470733a2f2f696d672e736869656c64732e696f2f747769747465722f666f6c6c6f772f546f6269536c6963652e7376673f7374796c653d736f6369616c)](https://twitter.com/TobiSlice)

Laravel Factory Classes is a package to help you creating Models with data through Factory Classes with a fluent API and automatic auto completion for your tests, seeder or everywhere you might want them.

### 💡 Why would you want to use this package?

[](#bulb-why-would-you-want-to-use-this-package)

Creating Factories for all your Models can get pretty messy sometimes. It lacks autocomplete, you often have to know about the business logic of the Model to get it to a specific state. A Factory class solves those problems. Every Model has its own Factory which contains everything necessary to create and fill it with data. You can simply use the API of the Factory to instantiate, save or alter the Model.
To know more about this topic I suggest [this blog post](https://stitcher.io/blog/laravel-beyond-crud-09-test-factories) from [sticher.io](sticher.io).

Example
-------

[](#example)

Think of a use case where we have a concert Model with a venue (belongsTo), supporting acts (belongsToMany), headliners (belongsToMany) and some other attributes we don't care about in this example. With normal Factories you would do something like this to create a concert:

```
$concert = factory(Concert::class)->create([
    'date' => now()
]);
$venue = factory(Venue::class)->create();
$headliner = factory(Artist::class)->create();
$support = factory(Artist::class)->create();

$concert->venue()->associate($venue);
$concert->lineup()->attach($headliner->id, ['headliner'=> true]);
$concert->lineup()->attach($support->id, ['headliner'=> false]);

$this->assert($concert, /*Do something with your Concert*/);
```

With this package it would look like this:

```
$concert = ConcertFatory::new()
            ->date(now())
            ->withVenue()
            ->withHeadliner()
            ->withSupport()
            ->create();

$this->assert($concert, /*Do something with your Concert*/);
```

and the Factory class looks like this:

```
