Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[8.0] Improve magic forXXX and hasXXX in model factories #2284

Closed
bastien-phi opened this issue Jul 3, 2020 · 0 comments
Closed

[8.0] Improve magic forXXX and hasXXX in model factories #2284

bastien-phi opened this issue Jul 3, 2020 · 0 comments

Comments

@bastien-phi
Copy link

Actually Illuminate\Database\Eloquent\Factories\Factory::__call method resolves factory using method name suffix i.e. calling UserFactory::new()->hasPosts(2) calls Factory::factoryForModel('post').

In the same time, assuming Post uses HasFactory, calling Post::factory() calls Factory::factoryForModel('Fully\Qualified\Post').

This makes a little bit harder to implement a custom Factory::$factoryNameResolver (required when models are not in app or app/Models or factories not in factory root namespace)

I think it should be possible to get the fully qualified class name with :

$relation = Str::camel(Str::substr($method,3));
$related = get_class($this->newModel()->{$relation}()->getRelated())

$factory = static::factoryForModel($related);

instead of

$factory = static::factoryForModel(Str::singular(Str::substr($method, 3)));

see https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Factories/Factory.php

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant