Laravel - Database - Factory And Model Discovery Conventions
Once you have defined your factories, you may use the static factory
method provided to your models by the Illuminate\Database\Eloquent\Factories\HasFactory
trait in order to instantiate a factory instance for that model.
The HasFactory
trait's factory
method will use conventions to determine the proper factory for the model the trait is assigned to. Specifically, the method will look for a factory in the Database\Factories
namespace that has a class name matching the model name and is suffixed with Factory
. If these conventions do not apply to your particular application or factory, you may overwrite the newFactory
method on your model to return an instance of the model's corresponding factory directly:
use Database\Factories\Administration\FlightFactory;
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return FlightFactory::new();
}
Next, define a model
property on the corresponding factory:
use App\Administration\Flight;
use Illuminate\Database\Eloquent\Factories\Factory;
class FlightFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Flight::class;
}