@ella-stinnes Hello, avoid doing any sql query in the definition(), if you create 20 tasks, it will loop 20 times
public function definition(): array
{
info('This is definition'); // Check the laravel.log
return [
'project_id' => Project::inRandomOrder()->first()->id, // move this to seeder or use state
'task_name' => $this->faker->sentence(6),
'sort_order' => Task::where('project_id', $project_id)->max('sort_order') + 1, // you can use Sequence
];
}
For your sort_order problem, you can use https://laravel.com/docs/11.x/eloquent-factories#factory-callbacks
use Illuminate\Database\Eloquent\Factories\Sequence;
// ...
public function configure(): static
{
info('This is configure'); // Check the laravel.log
return $this->sequence(function (Sequence $sequence) {
return [
'sort_order' => $sequence->index + 1,
];
});
}
public function definition(): array
{
info('This is definition'); // Check the laravel.log
return [
'project_id' => Project::factory(),
'task_name' => $this->faker->sentence(6),
// 'sort_order' => 0,
];
}