Laravel Waitlist:報告
當使用 taldres/laravel-waitlist 的應用程式需要關於其等候名單的數字時(例如儀表板、圖表、管理員小工具、每週報告),可使用此技能。
主要目標
- 從正確的來源回答每個問題:活動日誌用於「發生了什麼事」,
snapshot()用於「現在誰在名單上」
工作流程
1. 選擇資料來源
Waitlist::for('beta')->report()->between('2026-08-01', '2026-08-31')->totals();
// "230 人在八月確認":活動日誌,過去的事實
Waitlist::for('beta')->snapshot();
// "197 人目前正在名單上":地址,目前的事實
後續的清除動作永遠不會改變第一個數字。計數是步驟而不是人:離開後又回來的人會被確認兩次。
2. 當前狀態:snapshot()
$snapshot = Waitlist::for('beta')->snapshot();
$snapshot->pending; // 等待確認中
$snapshot->confirmed; // 已在名單上
$snapshot->unsubscribed; // 已離開,仍在保留期限內
$snapshot->active(); // pending + confirmed
$snapshot->total();
$snapshot->toArray(); // 已準備好 JSON 格式,包含專案和名單
3. 指定期間:總數
use Taldres\Waitlist\Enums\ActivityType;
use Taldres\Waitlist\Enums\EntryStatus;
$totals = Waitlist::for('beta')->report()->since(30)->totals(); // 最近 30 天
$totals->signups(); // 已訂閱 + 重新訂閱
$totals->of(ActivityType::Confirmed);
$totals->of(ActivityType::Unsubscribed, from: EntryStatus::Confirmed); // 確認後離開
$totals->confirmationRate(); // ?float,無人註冊時為 null
$totals->confirmedChange(); // 確認人數的淨變化量
4. 圖表:每日系列
$series = Waitlist::for('beta')->report()->since(30)->daily()->fillGaps();
foreach ($series as $day) {
$day->date; // CarbonImmutable
$day->signups();
$day->of(ActivityType::Confirmed);
}
$series->toArray(); // [{date, subscribed, resubscribed, confirmation_requested, confirmation_mailed, confirmed, unsubscribed, expired, erased, consent_granted, consent_withdrawn}]
fillGaps() 會填入沒有活動的日期,這對圖表來說是必要的。
5. 過去某一天的確認人數
Waitlist::for('beta')->report()->confirmedOn('2026-09-30'); // 在該日結束時
精確度取決於日誌可追溯的範圍,精確到天。今天的值等於 snapshot()->confirmed。
6. 範圍控制
Waitlist::report():預設專案中的所有名單;->forList('beta', 'launch')可縮小範圍。Waitlist::for('beta')->report():單一名單。Waitlist::project('acme')->report():單一專案;Waitlist::allProjects()->report():所有專案。->forPurpose('newsletter'):僅限某個選用目的的授權與撤銷;註冊、確認與離開不帶目的,因此會被排除。- 若未使用
since()或between(),則包含所有儲存的資料。
規則、參考與模板
- 一天總是以
app.timezone結束:每一行在寫入時儲存其所屬的日期 (occurred_on),報告會以此分組並在該時區解析其期間。清除動作會清除確切時間,因此在第一次註冊前設定好app.timezone;之後再更改只會影響後續的資料列,而不會重新設定既有資料列的日期。若使用 UTC,柏林夏季時間 23:30 的註冊會屬於下一個 UTC 日:這是一個明確的邊界,而非計數錯誤。 - 報告是一次分組查詢;如果儀表板頻繁輪詢,請在應用程式中快取它們。
- 剩餘的日誌會被最小化,而非匿名化:為需要它們的人保留報告。
- 指南:https://github.com/Taldres/laravel-waitlist/blob/main/docs/reporting.md
範例
- 「在管理員頁面顯示過去一個月的每日註冊數」:步驟 4 以 JSON 格式提供給圖表庫。
- 「每個月底我們有多少確認的人數?」:每個月底使用
confirmedOn()。 - 「本週 beta 名單的轉換率」:
report()->since(7)->totals()->confirmationRate()。
反模式
- 計算過去期間的
WaitlistEntry資料列;清除與保留會改變它們。 - 將
Waitlist::for($list)->count()視為「在名單上」:它包含了已離開的人;請使用snapshot()->confirmed或active()。 - 使用引擎特定的函數在 SQL 中計算日期;日誌已經儲存了報告日期。
- 期望經過目的過濾的報告包含註冊或確認資料。