Skip to content

Commit fd4d280

Browse files
authored
[LARAVEL 8] include_factory_builders does not work anymore (#1047) (#1049)
* [LARAVEL 8] include_factory_builders does not work anymore (#1047) * Change skip expression Append comment about deprecated to config file Append comment about deprecated to README file
1 parent 2ff9672 commit fd4d280

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ Then run `php artisan ide-helper:generate`, you will now see all Fluent methods
256256

257257
If you would like the `factory()->create()` and `factory()->make()` methods to return the correct model class,
258258
you can enable custom factory builders with the `include_factory_builders` line your `config/ide-helper.php` file.
259+
Deprecated for Laravel 8 or latest.
259260

260261
```php
261262
'include_factory_builders' => true,

config/ide-helper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
| Set to true to generate factory generators for better factory()
4646
| method auto-completion.
4747
|
48+
| Deprecated for Laravel 8 or latest.
49+
|
4850
*/
4951

5052
'include_factory_builders' => false,

src/Factories.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,25 @@ public static function all()
1212
{
1313
$factories = [];
1414

15-
$factory = app(Factory::class);
15+
if (static::isLaravelSevenOrLower()) {
16+
$factory = app(Factory::class);
1617

17-
$definitions = (new ReflectionClass(Factory::class))->getProperty('definitions');
18-
$definitions->setAccessible(true);
18+
$definitions = (new ReflectionClass(Factory::class))->getProperty('definitions');
19+
$definitions->setAccessible(true);
1920

20-
foreach ($definitions->getValue($factory) as $factory_target => $config) {
21-
try {
22-
$factories[] = new ReflectionClass($factory_target);
23-
} catch (Exception $exception) {
21+
foreach ($definitions->getValue($factory) as $factory_target => $config) {
22+
try {
23+
$factories[] = new ReflectionClass($factory_target);
24+
} catch (Exception $exception) {
25+
}
2426
}
2527
}
2628

2729
return $factories;
2830
}
31+
32+
protected static function isLaravelSevenOrLower()
33+
{
34+
return class_exists('Illuminate\Database\Eloquent\Factory');
35+
}
2936
}

tests/Factories/AllTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Barryvdh\LaravelIdeHelper\Tests\Factories;
6+
7+
use Barryvdh\LaravelIdeHelper\Factories;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class AllTest extends TestCase
11+
{
12+
public function testAll(): void
13+
{
14+
$factories = Factories::all();
15+
16+
self::assertEmpty($factories);
17+
}
18+
}

0 commit comments

Comments
 (0)