Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models and factories in subdirectories #39467

Closed
nikhiltri opened this issue Nov 4, 2021 · 2 comments
Closed

Models and factories in subdirectories #39467

nikhiltri opened this issue Nov 4, 2021 · 2 comments

Comments

@nikhiltri
Copy link

nikhiltri commented Nov 4, 2021

  • Laravel Version: 8.69.0
  • PHP Version: 7.4.24
  • Database Driver & Version: MySQL 5.7.32

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/

  • 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?

@aanfarhan
Copy link

aanfarhan commented Nov 4, 2021

Did you set $model properties in your Factory class with the Model::class ?
Something like this:

namespace Database\Factories\Collections;

use App\Models\Collections\Artist;
use Illuminate\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
     */
    public function definition()
    {
        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.

@nikhiltri
Copy link
Author

Yup, that did it! Thanks!

My factories are not in the app/ directory. The formatting of my post got a little messed up. I corrected the original post.

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

No branches or pull requests

2 participants