You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have models in subdirectories, and factories in corresponding subdirectories. The method to find a factory by a model works as it should with the subdirectory added to the namespace. But finding a model by a factory name ignores the extra directory in the namespace and breaks.
Steps To Reproduce:
This is in a private repository so I'm not able to share a link. But here's an abbreviated version of what my directory structure looks like:
app/
Models/
Collections/
Artwork.php
Artist.php
Exhibition.php
Shop/
Products.php
Category.php
database/
factories
Collections/
Artwork.php
Artist.php
Exhibition.php
Shop/
Products.php
Category.php
The method to get the factory name from a model resolveFactoryName() works as expected since it gets all parts of FQCN after App\Models/. But the reverse method modelName() breaks because line 678 only gives it the class name to append to App/Models. It doesn't know the value of the subdirectory to use to find the factory name. I tried flattening my factories into one directory, but then resolveFactoryName() breaks because it's looking for the factory in a subdirectory that it's no longer in.
It seems these methods will only work if all my models are flattened into one directory, same with my factories. I have about 50 models are prefer to keep them organized in subdirectories. Is it possible to get these two functions working in my context?
The text was updated successfully, but these errors were encountered:
Did you set $model properties in your Factory class with the Model::class ?
Something like this:
namespaceDatabase\Factories\Collections;
useApp\Models\Collections\Artist;
useIlluminate\Database\Eloquent\Factories\Factory;
class ArtistFactory extends Factory
{
/** * The name of the factory's corresponding model. * * @var string|null */protected$model = Artist::class;
/** * Define the model's default state. * * @return array */publicfunctiondefinition()
{
return [
//
];
}
}
And why your factories directory inside the app ? Shouldn't it placed inside database/factories ?
After some digging I found out that when you generate Factory using artisan command by default it's not setting the $model properties because of this changes #39310.
So when generate Model & Factory inside subdirectory using artisan command we should manually set the $model properties inside our factory class.
Description:
I have models in subdirectories, and factories in corresponding subdirectories. The method to find a factory by a model works as it should with the subdirectory added to the namespace. But finding a model by a factory name ignores the extra directory in the namespace and breaks.
Steps To Reproduce:
This is in a private repository so I'm not able to share a link. But here's an abbreviated version of what my directory structure looks like:
app/
database/
The method to get the factory name from a model
resolveFactoryName()
works as expected since it gets all parts of FQCN afterApp\Models/
. But the reverse methodmodelName()
breaks because line 678 only gives it the class name to append toApp/Models
. It doesn't know the value of the subdirectory to use to find the factory name. I tried flattening my factories into one directory, but thenresolveFactoryName()
breaks because it's looking for the factory in a subdirectory that it's no longer in.It seems these methods will only work if all my models are flattened into one directory, same with my factories. I have about 50 models are prefer to keep them organized in subdirectories. Is it possible to get these two functions working in my context?
The text was updated successfully, but these errors were encountered: