• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Model factory generator for Laravel

Laravel Factory Generator

Packagist Packagist License

Banner

Automatically generate factories from your existing models.

It will allow you to write tests containing your models much faster.

Installation

You can install the package via composer:

composer require thedoctor0/laravel-factory-generator --dev

For Laravel 6.x and 7.x check the v1.2.5.

Usage

To generate all factories at once, simply run this artisan command:

php artisan generate:factory

It will find all models and generate test factories based on the database structure and model relations.

Example

Migration and Model

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('username');
    $table->string('email')->unique();
    $table->string('password', 60);
    $table->integer('company_id');
    $table->rememberToken();
    $table->timestamps();
});

class User extends Model {
    public function company()
    {
        return $this->belongsTo(Company::class);
    }
}

Generated Factory

<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Contact;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<\App\Models\User>
 */
final class UserFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = User::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition(): array
    {
        return [
            'name' => faker()->name,
            'username' => faker()->userName,
            'email' => faker()->safeEmail,
            'password' => bcrypt(faker()->password),
            'company_id' => \App\Company::factory(),
            'remember_token' => Str::random(10),
        ];
    }
}

Advanced usage

Selecting models

To generate a factory for only specific model or models, run the artisan command:

php artisan generate:factory User Company

Overwriting existing factories

By default, generation will not overwrite any existing model factories.

You can force overwriting existing model factories by using the --force option:

php artisan generate:factory --force

Customizing the output directory

By default, it will search recursively for models under the app/Models directory.

If your models are within a different folder, you can specify this using --dir option.

In this case, run the artisan command:

php artisan generate:factory --dir app/Models

Customizing the namespace

If your models are within a different namespace, you can specify it using --namespace option.

You just need to execute this artisan command:

php artisan generate:factory --dir vendor/package/src/Models --namespace CustomNamespace\\Models

Using recursive mode

By default, your model directory structure is not taken into account, even though it has subdirectories.

You can reflect it to database/factories directory by using the --recursive option:

php artisan generate:factory --recursive

Customizing the factory template

If you want you can customize the factory template to suit your needs.

To publish a factory template to resources/views/vendor/factory-generator/factory.blade.php, run the artisan command:

php artisan vendor:publish --tag="factory-generator"

License

The MIT License (MIT). Please see license file for more information.

More Repositories

1

zip-release

GitHub action that can be used to create release zip archive.
Shell
130
star
2

CSGOMod

CS:GO Mod for Counter-Strike 1.6 (AMXX 1.8.3 / 1.9 / 1.10).
Pawn
55
star
3

laravel-mailjet-driver

Laravel mail driver package for Mailjet and wrapper for its API
PHP
33
star
4

openvas-docker-lite

OpenVAS docker container with custom automation script.
Shell
29
star
5

AMXXLegacy

Various AmxModX plugins written / modified / aquired over the years.
Pawn
11
star
6

CoDMod

Call of Duty Mod for Counter-Strike 1.6 (AMXX 1.8.3 / 1.9 / 1.10).
Pawn
10
star
7

BattlefieldOne

Battlefield One Mod for Counter-Strike 1.6
Pawn
7
star
8

CSCOInstaller

CSCO Installer - simple installer and updater for Counter-Strike: Classic Offensive.
C#
6
star
9

UltimateStats

Advanced players statistics plugin for Counter-Strike 1.6 (AMXX 1.8.3 / 1.9).
Pawn
6
star
10

YouTubeQuery

Reworked search for YouTube videos in Wox Launcher.
C#
5
star
11

inertia-vue-typescript-example

PHP
5
star
12

leetcode-golang

Solutions for LeetCode problems written in Golang (Go).
Go
4
star
13

vue-input-mask

Input mask directive for Vue.js - lightweight, dependency free, written in TypeScript.
TypeScript
4
star
14

AdminPanel

Centralized Admins Panel for Counter-Strike 1.6 Servers.
PHP
4
star
15

phpstorm-ide-config

PHPStorm IDE config and shortcuts cheat sheet.
HTML
3
star
16

SublimeAMXXEditor

Sublime Text 3 plugin for AMXX development.
Python
3
star
17

NotAHotDog

Image classification using TensorFlow (Inception v3).
Python
3
star
18

Motus

Experimental blockchain implementation in Javascript.
JavaScript
3
star
19

laravel-stubs

Opinionated versions of the Laravel stubs for Artisan.
PHP
3
star
20

DiabloMod

Classic version of DiabloMod for Counter-Strike 1.6
SourcePawn
3
star
21

algods

Golang algorithms and data structures.
Go
2
star
22

WebDevShell

Sublime Text 3 plugin for executing shell commands related to web apps development: Laravel Artisan, Composer, NPM, Yarn, Python, PHP, PHPStan, Psalm, ESLint and more!
Python
2
star
23

laravel-test-factory-helper-pr

PHP
2
star
24

tailwind-html-template

Template for basic projects with HTML5, CSS (Tailwind CSS) and vanilla JavaScript (ES6+).
HTML
1
star
25

MonitorAndRecord

C
1
star