๐น๏ธ Full-Stack Developer
๐ฉ Poland
๐ช [email protected]
๐ LinkedIn Profile
Model factory generator for Laravel
License: MIT License
๐น๏ธ Full-Stack Developer
๐ฉ Poland
๐ช [email protected]
๐ LinkedIn Profile
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?
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
Hi,
When I run the php artisan generate:factory
command 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?
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);
}
}
$table->enum('project_status', Project::$STATUS)->default(Project::$STATUS[0])
The above produced $faker instead of $this->faker
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.
Getting Base table or view not found
an error in case if we use table prefix in database
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
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}
"}
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
https://laravel-news.com/laravel-9-18-0
https://laravel.com/docs/9.x/helpers#method-fake
so
'name' => $this->faker->name
can now be shortened to
'name' => fake()->name
version_compare(app()->version(), '9.18.0', '>=');
:)
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()
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.
it would be nice if the generator understood it :)
https://tenancyforlaravel.com/docs/v3/multi-database-tenancy
https://tenancyforlaravel.com/docs/v3/migrations
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?
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.
It doesn't work on a modular laravel project.
It's probably right time to add some tests.
GitHub action can be used to setup MySQL and PostgreSQL databases in containers.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.