2025-10-21 17:23:54 -06:00

94 lines
2.2 KiB
PHP

<?php
namespace Database\Factories;
use App\Models\Record;
use App\Models\Vehicle;
use App\Models\User;
use App\Models\Error;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Record>
*/
class RecordFactory extends Factory
{
protected $model = Record::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
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,
]);
}
}