Skip to content

Document optional model property by default for factories #7388

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions database-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ To see an example of how to write a factory, take a look at the `database/factor

class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;

/**
* Define the model's default state.
*
Expand All @@ -97,7 +90,16 @@ To see an example of how to write a factory, take a look at the `database/factor
}
}

As you can see, in their most basic form, factories are classes that extend Laravel's base factory class and define a `model` property and `definition` method. The `definition` method returns the default set of attribute values that should be applied when creating a model using the factory.
As you can see, in their most basic form, factories are classes that extend Laravel's base factory class and `definition` method. The `definition` method returns the default set of attribute values that should be applied when creating a model using the factory. By default, factories guess the model class which you can override by defining a `model` property on the factory class.

use App\Models\User;

/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;

Via the `faker` property, factories have access to the [Faker](https://github.com/FakerPHP/Faker) PHP library, which allows you to conveniently generate various kinds of random data for testing.

Expand Down Expand Up @@ -150,13 +152,6 @@ Factory callbacks are registered using the `afterMaking` and `afterCreating` met

class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;

/**
* Configure the model factory.
*
Expand Down