token = Str::random(40); $token->user_id = $userId; $token->email = $email; $token->created_at = Carbon::now(); return $token; } /** * Define the relationship with the owner of this email token. * * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ public function user() { return $this->belongsTo(User::class); } /** * Find the token with the given ID, and assert that it has not expired. * * @param \Illuminate\Database\Eloquent\Builder $query * @param string $id * @return static * @throws InvalidConfirmationTokenException */ public function scopeValidOrFail($query, $id) { /** @var EmailToken $token */ $token = $query->find($id); if (! $token || $token->created_at->diffInDays() >= 1) { throw new InvalidConfirmationTokenException; } return $token; } }