*/ class DownloadLogFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = DownloadLog::class; /** * Define the model's default state. * * @return array */ public function definition(): array { return [ 'document_id' => Document::factory(), 'user_id' => User::factory(), 'downloaded_at' => fake()->dateTimeBetween('-1 year', 'now'), 'ip_address' => fake()->ipv4(), ]; } /** * 指定下载日志使用特定的文档 */ public function forDocument(Document|int $document): static { $documentId = $document instanceof Document ? $document->id : $document; return $this->state(fn (array $attributes) => [ 'document_id' => $documentId, ]); } /** * 指定下载日志使用特定的用户 */ public function forUser(User|int $user): static { $userId = $user instanceof User ? $user->id : $user; return $this->state(fn (array $attributes) => [ 'user_id' => $userId, ]); } /** * 指定下载日志使用最近的时间 */ public function recent(): static { return $this->state(fn (array $attributes) => [ 'downloaded_at' => fake()->dateTimeBetween('-7 days', 'now'), ]); } }