PHPackages                             kfirba/factor - 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. kfirba/factor

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

kfirba/factor
=============

Short and expressive way to use Laravel's model factories.

v1.0.1(8y ago)18MITPHP

Since Feb 21Pushed 8y ago1 watchersCompare

[ Source](https://github.com/kfirba/factor)[ Packagist](https://packagist.org/packages/kfirba/factor)[ RSS](/packages/kfirba-factor/feed)WikiDiscussions master Synced 4d ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

Factor
======

[](#factor)

Short and expressive way to use Laravel's model factories.

Install
-------

[](#install)

```
$ composer require --dev kfirba/factor
```

Usage
-----

[](#usage)

> I've written a more [detailed blog about this package](https://kfirba.me/blog/factor-short-and-expressive-way-to-use-laravels-factories). Make sure to check it out!

The package registers 2 global methods:

1. `make($model, $overrides = [], $times = 1)`
2. `create($model, $overrides = [], $times = 1)`

These methods are almost identical to the `create()` and `make()` method that the `Illuminate\Database\Eloquent\Factory` offers (usually you use the `factory()` global method to obtain an instance of the factory).

> The examples below are the same for both the `create()` and `make()` methods. The only difference is that the `create()` method will also persist the generated instances.

```
$user = make(User::class);
// -> Equivalent to `factory(User::class)->make()`

$user = make(User::class, $overrides);
// -> Equivalent to `factory(User::class)->make($overrides);`

$user = make(User::class, $overrides, $times);
// -> Equivalent to `factory(User::class, $times)->make($overrides);
```

It looks like that `create()` and `make()` methods are just simple aliases to the `factory()->{$method}()`, but this is where it gets interesting.

I Want to Decide!
-----------------

[](#i-want-to-decide)

It may sometimes feel "right" to pass the number of objects we want to build as the second argument instead of the overrides. Factor lets you do that. If you pass an integer as the second argument, it will assume you are trying to set the number of objects to create:

```
make(User::class, 3);
// Equivalent to:
make(User::class, [], 3);

make(User::class, 3, ['name' => 'John']);
// Equivalent to:
make(User::class, ['name' => 'John'], 3);
```

States
------

[](#states)

So we decided we want to use a shorthand to the `factory()->create()` call, however, what happens when we want to give it a certain state? The simplest solution is to tweak the `make()` and `create()` methods a little bit and make it accepts an additional argument.

However, the argument list gets pretty long and given that both the `$overrides` and the `$times` arguments are optionals, it makes it a mess to do it that way.

This package lets you use an expressive syntax to declare your states:

```
// Given the factory definition [name => 'Fake User'] and the state 'admin' [name => 'Admin']:
Class User
{
    public function callMe() { echo 'called'; }
}

$user = make(User::class);
echo $user->name;
// -> 'Fake User'
echo $user->callMe();
// -> 'called';

$admin = make(User::class)->states('admin');
echo $admin->name;
// -> 'Admin'
echo $admin->callMe();
// -> 'called';
```

Pay close attention to the above `admin` state. We call the `states()` method directly on the `make()` returned value and we are still able to access any property or method off of the actual instance. **This is exactly the added bonus this package offers.**

small Caveat (Immediate Creation of Models)
-------------------------------------------

[](#small-caveat-immediate-creation-of-models)

Sometimes we want to create models just as a part of the test's setup but we never really need to directly interact with them. Factor will not immediately create the models but will decide in real-time when they need to be created. This behavior will break your tests if you rely on the models to exist in the database only. To overcome this shortcoming, Factor also provides a `now()` method which will let you use it's elegant and expressive syntax but tell to immediately create the models:

```
create(User::class);
// -> 'Kfirba\Factor\PendingModel'
create(User::class)->now();
// -> 'App\User'

create(User::class)->states('admin');
// -> 'Kfirba\Factor\PendingModel'
create(User::class)->states('admin')->now();
// -> 'App\User'
```

License
-------

[](#license)

Factor is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

3005d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/00b97262a98fa4f5375e21a881c2b43493f4fd60067cea73669365fe60da4c79?d=identicon)[kfirba](/maintainers/kfirba)

---

Top Contributors

[![kfirba](https://avatars.githubusercontent.com/u/8510486?v=4)](https://github.com/kfirba "kfirba (9 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/kfirba-factor/health.svg)

```
[![Health](https://phpackages.com/badges/kfirba-factor/health.svg)](https://phpackages.com/packages/kfirba-factor)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[abbasudo/laravel-purity

elegant way to add filter and sort in laravel

514330.5k1](/packages/abbasudo-laravel-purity)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[dragon-code/laravel-deploy-operations

Performing any actions during the deployment process

240173.5k2](/packages/dragon-code-laravel-deploy-operations)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
