Skip to content

Models and factories in subdirectories #39467

@nikhiltri

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions