*/ class RecordFactory extends Factory { protected $model = Record::class; /** * Define the model's default state. * * @return array */ public function definition(): array { return [ 'folio' => $this->generateFolio(), 'vehicle_id' => Vehicle::factory(), 'user_id' => User::inRandomOrder()->first()?->id ?? User::factory(), 'error_id' => fake()->boolean(10) ? Error::factory() : null, // 10% con error ]; } /** * Generate a unique record folio */ private function generateFolio(): string { $year = now()->year; $number = fake()->unique()->numerify('######'); return 'EXP-' . $year . '-' . $number; } /** * Indicate that the record has an error */ public function withError(): static { return $this->state(fn (array $attributes) => [ 'error_id' => Error::factory(), ]); } /** * Indicate that the record has no error */ public function withoutError(): static { return $this->state(fn (array $attributes) => [ 'error_id' => null, ]); } /** * Indicate that the record belongs to a specific vehicle */ public function forVehicle(int $vehicleId): static { return $this->state(fn (array $attributes) => [ 'vehicle_id' => $vehicleId, ]); } /** * Indicate that the record belongs to a specific user */ public function forUser(int $userId): static { return $this->state(fn (array $attributes) => [ 'user_id' => $userId, ]); } /** * Indicate that the record has a specific error */ public function withSpecificError(int $errorId): static { return $this->state(fn (array $attributes) => [ 'error_id' => $errorId, ]); } }