Factories
Factories for Models are created automatically for Quick Models and Custom Models.
When creating models, the HasFactory
trait will be automatically included in the model and documented via its PHPDoc.
Soft Deletes comes free
Laravel includes the trashed
state when your model uses soft-deletes.
models:
Post:
title: string
slug: string
body: longText
published_at: timestamp nullable
length: int
softDeletes: ~
2
3
4
5
6
7
8
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* ...
* @method static \Database\Factories\PostFactory factory($count = null, $state = [])
*/
class Post extends Model
{
use HasFactory;
use SoftDeletes;
// ...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @implements \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class PostFactory extends Factory
{
protected $model = Post::class;
public function definition()
{
throw new \RuntimeException("The factory for model [Post] has no defined attributes yet.");
// return [
// // ...
// ];
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
I will remember you
Larawiz automatically adds Exceptions to the definition and the factory states, so you can remember to fill the attributes before anything else.
No Model Factory
You can disable factories by issuing the factory
key with the false
value. This will also disable including the HasFactory
trait in the model.
models:
Post:
title: string
body: longText
published_at: timestamp useCurrent
factory: false
2
3
4
5
6
7
No Factory, no Seeder
If you disable the Factory of the Model, it will also disable the Seeder for it.
Factory States
You can add additional factory states by defining a list on the factory
key with the state names you wish to add.
models:
Post:
title: string
body: longText
factory:
- unpublished
- scheduled
2
3
4
5
6
7
8
public function definition()
{
// ...
}
public function unpublished()
{
return $this->state(static function (array $attributes): array {
throw new \RuntimeException("The factory for model [$model->name] has no defined the [$state] state yet.");
// return [
// // ...
// ];
});
}
public function scheduled()
{
return $this->state(static function (array $attributes): array {
throw new \RuntimeException("The factory for model [$model->name] has no defined the [$state] state yet.");
// return [
// // ...
// ];
});
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
States will be created empty with an exception to remember to setting the apropiate attributes.