diff --git a/CHANGELOG.md b/CHANGELOG.md index d85e760..78d54fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. - Model hooks for adding custom information from external sources to model classes through the ModelsCommand [\#945 / wimski](https://github.com/barryvdh/laravel-ide-helper/pull/945) ### Fixed +- Fix ide-helper:models exception if model doesn't have factory [\#1196 / ahmed-aliraqi](https://github.com/barryvdh/laravel-ide-helper/pull/1196) - Running tests triggering post_migrate hooks [\#1193 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1193) - Array_merge error when config is cached prior to package install [\#1184 / netpok](https://github.com/barryvdh/laravel-ide-helper/pull/1184) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 4ee4b08..8dcbb23 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -1097,7 +1097,14 @@ class ModelsCommand extends Command } $modelName = get_class($model); - $factory = get_class($modelName::factory()); + $modelBaseName = class_basename($modelName); + + $factory = "\Database\Factories\\{$modelBaseName}Factory"; + + if ($modelName::newFactory()) { + $factory = get_class($modelName::newFactory()); + } + $factory = '\\' . trim($factory, '\\'); if (!class_exists($factory)) { diff --git a/tests/Console/ModelsCommand/Factories/Models/ModelWithoutFactory.php b/tests/Console/ModelsCommand/Factories/Models/ModelWithoutFactory.php new file mode 100644 index 0000000..d88ca6b --- /dev/null +++ b/tests/Console/ModelsCommand/Factories/Models/ModelWithoutFactory.php @@ -0,0 +1,13 @@ +assertSame(0, $tester->getStatusCode()); $this->assertStringContainsString('Written new phpDocBlock to', $tester->getDisplay()); + $this->assertStringNotContainsString('not found', $tester->getDisplay()); $this->assertMatchesMockedSnapshot(); } } diff --git a/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php index df8bef1..4d8b0f8 100644 --- a/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php +++ b/tests/Console/ModelsCommand/Factories/__snapshots__/Test__test__1.php @@ -4,6 +4,27 @@ declare(strict_types=1); namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; + +/** + * Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\Factories\Models\ModelWithoutFactory + * + * @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newModelQuery() + * @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory newQuery() + * @method static \Illuminate\Database\Eloquent\Builder|ModelWithoutFactory query() + * @mixin \Eloquent + */ +class ModelWithoutFactory extends Model +{ + use HasFactory; +} +