Git Product home page Git Product logo

laravel-factory-generator's Introduction

laravel-factory-generator's People

Contributors

antevgen avatar demianottema avatar h-ishak avatar laravel-shift avatar sajaddp avatar sankarsuda avatar spawnia avatar thedoctor0 avatar wadakatu avatar xedinunknown avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-factory-generator's Issues

Empty Factory generated

Hi,
I have a migration existing in the migrations table for many models but when I run the command
php artisan generate:factory
It generates empty factories for all models.
Shouldn't it include all columns of specific table defined in factory?

Laravel 11.x

I tried installing it my Laravel 11.x project, and i know it not released yet, but it can already be used.

I don't see any potential issues, if i have the time, i will submit a PR

syntax error, unexpected token ","

Hi,

When I run the php artisan generate:factorycommand I get the following error:

Could not analyze class App\Models\Contact.
Exception: syntax error, unexpected token "," (View: .../vendor/thedoctor0/laravel-factory-generator/resources/views/class-factory.blade.php)

Migration:

    public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email');
            $table->string('phone');
            $table->softDeletes();
            $table->timestamps();
        });
    }

Model:

class Contact extends Model
{
    use SoftDeletes, HasFactory;

    const ID = 'id';

    const FIRST_NAME = 'first_name';

    const LAST_NAME = 'last_name';

    const EMAIL = 'email';

    const PHONE = 'phone';

    const CREATED_AT = 'created_at';

    const UPDATED_AT = 'updated_at';

    protected $casts = [
        self::ID => 'int',
    ];

    protected $dates = [
        self::CREATED_AT,
        self::UPDATED_AT,
    ];

    protected $fillable = [
        self::FIRST_NAME,
        self::LAST_NAME,
        self::EMAIL,
        self::PHONE,
    ];
}

Artisan About:

Environment ....................................................
  Laravel Version ......................................... 9.24.0
  PHP Version .............................................. 8.1.8
  Composer Version ........................................ 2.3.10
  Environment .............................................. local
  Debug Mode ............................................. ENABLED
  Maintenance Mode ........................................... OFF

  Cache ..........................................................
  Config .............................................. NOT CACHED
  Events .............................................. NOT CACHED
  Routes .............................................. NOT CACHED
  Views ................................................... CACHED

  Drivers ........................................................
  Broadcasting ............................................... log
  Cache ..................................................... file
  Database ................................................ sqlite
  Logs ..................................................... daily
  Mail ...................................................... smtp
  Queue ..................................................... sync
  Session ................................................. cookie

What's wrong?

Error: model is not configured properly!

When I try to run php artisan generate:factory User Companies
I get the following error Database driver (mysql) for App\Models\Companies model is not configured properly!

I get the same error on all models when running php artisan generate:factory

Companies migration

Schema::create('companies', function (Blueprint $table) {
    $table->id();
    $table->string('company_name', 100);
    $table->unsignedBigInteger('user_id')->index();
    $table->timestamps();
});

Schema::table('companies', function (Blueprint $table) {
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

Companies Model

class Companies extends Model
{
    use HasFactory;
    public $guarded = [];

    public function clusters(): HasMany {
        return $this->hasMany(PosClusters::class);
    }

    public function user(): BelongsTo {
        return $this->belongsTo(User::class);
    }
}

ENUM issue

$table->enum('project_status', Project::$STATUS)->default(Project::$STATUS[0])
The above produced $faker instead of $this->faker

Add support for Lumen versioning

When creating the factories, the isLaravel8OrAbove version check always returns false for Lumen, as the version string is Lumen (8.2.4) (Laravel Components ^8.0) instead of just 8.2.4.

Would be great to take the Lumen version into account, to generate factory classes instead of methods.

Laravel 11 support

Hello, in Laravel 11 they removed dependency on doctrine/dbal and factory generator doesn't work correctly. It cannot get table structure anymore because it used DBAL before.
So there is an Exception

Method Illuminate\Database\MySqlConnection::getDoctrineSchemaManager does not exist.

in getPropertiesFromTable:210

PDO error when generator factories

I get this error when I try to generate factories.

[2023-02-17 14:36:55] local.ERROR: PDO::prepare(): Argument #1 ($query) must be of type string, Illuminate\Database\Query\Expression given {"exception":"[object] (TypeError(code: 0): PDO::prepare(): Argument #1 ($query) must be of type string, Illuminate\\Database\\Query\\Expression given at /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Database/Connection.php:414)
[stacktrace]
#0 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Database/Connection.php(414): PDO->prepare(Object(Illuminate\\Database\\Query\\Expression))
#1 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Database/Connection.php(753): Illuminate\\Database\\Connection->Illuminate\\Database\\{closure}(Object(Illuminate\\Database\\Query\\Expression), Array)
#2 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Database/Connection.php(720): Illuminate\\Database\\Connection->runQueryCallback(Object(Illuminate\\Database\\Query\\Expression), Array, Object(Closure))
#3 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Database/Connection.php(405): Illuminate\\Database\\Connection->run(Object(Illuminate\\Database\\Query\\Expression), Array, Object(Closure))
#4 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/thedoctor0/laravel-factory-generator/src/Database/EnumMysql.php(14): Illuminate\\Database\\Connection->select(Object(Illuminate\\Database\\Query\\Expression))
#5 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/thedoctor0/laravel-factory-generator/src/Database/EnumValues.php(21): TheDoctor0\\LaravelFactoryGenerator\\Database\\EnumMysql->values()
#6 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/thedoctor0/laravel-factory-generator/src/Database/EnumValues.php(13): TheDoctor0\\LaravelFactoryGenerator\\Database\\EnumValues->values(Object(App\\Models\\Landlord), 'user_id')
#7 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/thedoctor0/laravel-factory-generator/src/Console/GenerateFactoryCommand.php(314): TheDoctor0\\LaravelFactoryGenerator\\Database\\EnumValues::get(Object(App\\Models\\Landlord), 'user_id')
#8 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/thedoctor0/laravel-factory-generator/src/Console/GenerateFactoryCommand.php(264): TheDoctor0\\LaravelFactoryGenerator\\Console\\GenerateFactoryCommand->setProperty(Object(App\\Models\\Landlord), 'user_id', 'bigint')
#9 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/thedoctor0/laravel-factory-generator/src/Console/GenerateFactoryCommand.php(167): TheDoctor0\\LaravelFactoryGenerator\\Console\\GenerateFactoryCommand->getPropertiesFromTable(Object(App\\Models\\Landlord))
#10 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/thedoctor0/laravel-factory-generator/src/Console/GenerateFactoryCommand.php(113): TheDoctor0\\LaravelFactoryGenerator\\Console\\GenerateFactoryCommand->generateFactory('App\\\\Models\\\\Land...')
#11 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): TheDoctor0\\LaravelFactoryGenerator\\Console\\GenerateFactoryCommand->handle()
#12 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#13 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#14 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#15 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Container/Container.php(661): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#16 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Console/Command.php(194): Illuminate\\Container\\Container->call(Array)
#17 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/symfony/console/Command/Command.php(312): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#18 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Console/Command.php(163): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#19 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/symfony/console/Application.php(1022): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#20 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/symfony/console/Application.php(314): Symfony\\Component\\Console\\Application->doRunCommand(Object(TheDoctor0\\LaravelFactoryGenerator\\Console\\GenerateFactoryCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#21 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/symfony/console/Application.php(168): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#22 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Console/Application.php(102): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#23 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(155): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#24 /Users/arasivaneswaran/Documents/repos/perso/landlord-helper/artisan(35): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#25 {main}
"} 

Unable to correctly resolve path to database/factories directory

I'm having problems with generating factories - app is not able to resolve the path to the database folder correctly.

I've debugged to problem and found out, that you have hardoced the path to database/factories without proberly using Laravel's base_path() helper.

So instead of using:
$filename = "database/factories/{$class}Factory.php";
it should be (in my opinion) wrapped like this:
$filename = base_path("database/factories/{$class}Factory.php");

Would you mind changing this? It's a very easy fix.

Note that the path is hard-coded in three different places, so this needs to be reflected in all three places.

Many thanks

When running tests getting the error Doctrine\DBAL\DBALException

When running my tests I get this error until I uninstall the package

   Doctrine\DBAL\DBALException

  Type enum already exists.

  at vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php:262
    258โ–•      * @return DBALException
    259โ–•      */
    260โ–•     public static function typeExists($name)
    261โ–•     {
  โžœ 262โ–•         return new self('Type ' . $name . ' already exists.');
    263โ–•     }
    264โ–•
    265โ–•     /**
    266โ–•      * @param string $name

      +3 vendor frames
  4   [internal]:0
      TheDoctor0\LaravelFactoryGenerator\Console\GenerateFactoryCommand::__construct()

generating factories with tenancyforlaravel

when using tenancyforlaravel there are multiple db connections.

So when trying to find the tables connected to a model it needs to know in which context to look.
central and tenants have different db:s.
Ive solved this by switching app db when generating models on central and tenant.

  • central models -> central db
  • tenant models -> tenant db

it would be nice if the generator understood it :)

https://tenancyforlaravel.com/docs/v3/multi-database-tenancy
https://tenancyforlaravel.com/docs/v3/migrations

ref: dedoc/scramble#81 (comment)

Generate BelongsToMany in definition

I just installed the package and ran it (fantastic tool, btw). However, I noticed it didn't generate any of the BelongsToMany part of the relationships. Is that intentional?

Different namespace from --dir

Hi,

Thanks for taking this up!

I tried to specify --dir vendor/bilogic/package/src/Models/ which did not work.
I suspect the package assumes the same namespace. Possible to specify a namespace as well?

$ php artisan generate:factory --dir vendor/bilogic/package/src/Models/
Unable to find vendor\bilogic\package\src\Models\Transaction class!
Unable to find vendor\bilogic\package\src\Models\Customer class!

Perhaps allow us to specify the namespace after the folder?
Something like php artisan generate:factory --dir vendor/bilogic/package/src/Models/;Bilogic/Package/Models?

Sidenote:
As for touching the vendor folder, they are symlinked to the packages I'm developing.
So, I kinda know what I'm doing and just trying to use this package to create factories for my packages.

Tests

It's probably right time to add some tests.
GitHub action can be used to setup MySQL and PostgreSQL databases in containers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.