PHPackages                             makeabledk/laravel-factory-enhanced - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. makeabledk/laravel-factory-enhanced

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

makeabledk/laravel-factory-enhanced
===================================

v6.2.0(2mo ago)180120.0k↓51.7%92CC-BY-SA-4.0PHPPHP ^8.1CI passing

Since Feb 24Pushed 2mo ago5 watchersCompare

[ Source](https://github.com/makeabledk/laravel-factory-enhanced)[ Packagist](https://packagist.org/packages/makeabledk/laravel-factory-enhanced)[ RSS](/packages/makeabledk-laravel-factory-enhanced/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (12)Versions (25)Used By (2)

[![Laravel Factory Enhanced - Supercharge your tests](https://raw.githubusercontent.com/makeabledk/laravel-factory-enhanced/master/banner-1.png)](https://raw.githubusercontent.com/makeabledk/laravel-factory-enhanced/master/banner-1.png)

Laravel Factory Enhanced 🔥
==========================

[](#laravel-factory-enhanced-)

[![Latest Version on Packagist](https://camo.githubusercontent.com/3ecf08e8b45e06747208b9346c332ec46680ff9c92534ed0c63c904eb3520b7c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d616b6561626c65646b2f6c61726176656c2d666163746f72792d656e68616e6365642e737667)](https://packagist.org/packages/makeabledk/laravel-factory-enhanced)[![Build Status](https://camo.githubusercontent.com/11b54dfebde527dd9b195dbf6ea1980482c492bb974c6ef0829456410128ded4/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6d616b6561626c65646b2f6c61726176656c2d666163746f72792d656e68616e6365642f52756e25323074657374733f6c6162656c3d5465737473)](https://github.com/makeabledk/laravel-factory-enhanced/actions)[![StyleCI](https://camo.githubusercontent.com/b44eb5aa188237220c9077815269ec1db3fce309f6c8253f045a94343770fc2c/68747470733a2f2f7374796c6563692e696f2f7265706f732f3131373638303732322f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/117680722)

Bring the magic of eloquent relationships into the Laravel Factory.

Traditionally if you wanted to factory a team with some users, you'd have to manually create the individual team and users and then tie them together afterwards. This can easily lead to very verbose tests.

**Laravel 7.x and earlier**

```
$team = factory(Team::class)->create();
$users = factory(User::class)->times(2)->create();
foreach ($users as $user) {
    factory(TeamMember::class)->create([
        'team_id' => $team,
        'user_id' => $user,
        'role' => 'admin'
    ]);
}
```

**Laravel 8.0 and later**

```
$team = Team::factory()
    ->hasAttached(
        User::factory()->count(2),
        ['role' => 'admin']
    )
    ->create();
```

**Laravel Factory Enhanced**

```
$team = Team::factory()
    ->with(2, 'users', ['pivot.role' => 'admin'])
    ->create();
```

Installation
------------

[](#installation)

You may install the package via composer

```
composer require makeabledk/laravel-factory-enhanced
```

### Versions

[](#versions)

**Laravel 8+ class based factories**

Version 4 and later of this package is compatible with the new class-based syntax introduced with Laravel 8.

**Pre-Laravel 7 factories**

Version 3 and earlier of this package is compatible with the legacy `$factory->define()` syntax. Please find docs here [v3 documentation](https://github.com/makeabledk/laravel-factory-enhanced/tree/3.x):

Upgrade guide to v4
-------------------

[](#upgrade-guide-to-v4)

The majority of the refactoring needed to upgrade to v4 of this package, lies in rewriting factories to be compatible with Laravel class-based factories.

If you use [Laravel Shift](https://laravelshift.com) when upgrading to Laravel 8, a lot of this work will be automated, and you will be well on you way.

### Rewriting to class based factories

[](#rewriting-to-class-based-factories)

Please use [Laravel Shift](https://laravelshift.com) for upgrading Laravel versions, or refer to the [official documentation](https://laravel.com/docs/8.x/database-testing) on how to write factories using the class-based approach.

### Applying states

[](#applying-states)

Replace all occurrences of `->state('some-state')` with `->someState()` in your test suite.

### Presets

[](#presets)

The concept of presets which was introduced by this package may now simply be rewritten to states.

As such, replace all occurrences of `->preset('some-preset')` with `->somePreset()` in your test suite.

### Times method

[](#times-method)

Replace all occurrences of `->times(x)` with `->count(x)` in your test suite.

### Factory helper syntax

[](#factory-helper-syntax)

This change is completely optional. If you wish, you may change all occurrence of `factory(SomeModel::class)` to `SomeModel::factory()` in your test suite.

*If you choose to do so*, remember to add `use \Makeable\LaravelFactory\Factory;` to all models.

### Other breaking changes

[](#other-breaking-changes)

The `odds()` method has been removed from the Factory instance.

Usage
-----

[](#usage)

Once the package is installed, your factories should extend `Makeable\LaravelFactory\Factory` rather than the native Laravel `Factory` class.

Additionally, please make sure to use the corresponding `Makeable\LaravelFactory\HasFactory` trait on your models.

For example:

**app/Models/User.php**

```
