Files
cssd-back/database/factories/TransferFactory.php
T
2024-11-23 22:14:01 +03:30

82 lines
2.1 KiB
PHP
Executable File

<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\User;
class TransferFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
// Get a random model for polymorphic relationship
$transferableModels = [
\App\Models\Package::class,
\App\Models\Item::class,
];
$transferableType = $this->faker->randomElement($transferableModels);
$transferable = $transferableType::factory()->create();
// Generate from_date and ensure to_date is after from_date
$fromDate = $this->faker->dateTimeBetween('-1 year', 'now');
$toDate = $this->faker->dateTimeBetween($fromDate, '+1 year');
return [
'transferable_type' => $transferableType,
'transferable_id' => $transferable->id,
'user_id' => User::factory(),
'from_date' => $fromDate,
'to_date' => $toDate,
'status' => $this->faker->randomElement(['approved', 'rejected', 'suspended']),
'user_transfer_id' => User::factory(),
'deleted_at' => null,
];
}
/**
* Indicate that the transfer is approved.
*/
public function approved(): static
{
return $this->state(fn (array $attributes) => [
'status' => 'approved',
]);
}
/**
* Indicate that the transfer is rejected.
*/
public function rejected(): static
{
return $this->state(fn (array $attributes) => [
'status' => 'rejected',
]);
}
/**
* Indicate that the transfer is suspended.
*/
public function suspended(): static
{
return $this->state(fn (array $attributes) => [
'status' => 'suspended',
]);
}
/**
* Indicate that the transfer is soft deleted.
*/
public function deleted(): static
{
return $this->state(fn (array $attributes) => [
'deleted_at' => now(),
]);
}
}