簡體   English   中英

laravel eloquent 有多個 where 條件問題

[英]laravel eloquent with multiple where conditions issue

在我基於 laravel 的應用程序數據庫中,我有一個表來存儲用戶付款的記錄。

我有 created_at 列來存儲記錄創建日期和 payment_annum 列來存儲訂閱類型(每月或每年)

現在我正在嘗試在我的管理儀表板刀片上顯示這些記錄。 但問題是我只需要顯示上個月的每月訂閱記錄。

到目前為止,我可以檢索訂閱類型為每月但在上個月之前難以過濾的數據。

這是我目前的 eloquent,這里只檢查訂閱類型

$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
        ->get();

那么我怎樣才能檢查“上個月”的情況呢

嘗試將您的 eloquent 更改為此,

$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
        ->whereMonth('created_at', '=', Carbon::now()->subMonth()->month)
        ->get();

這會給你最后一個月的記錄,其中 payment_annum 是每月

您希望查詢返回上個月創建的所有元素(截至今天: created_by匹配2020-03-* )還是上個月創建created_by >= 2020-03-23 )?

對於 3 月,將接受的解決方案擴展為

$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
        ->whereMonth('created_at', '=', Carbon::now()->subMonth()->month)
        ->whereYear('created_at', '=', Carbon::now()->subMonth()->year)
        ->get();

對於后者,這應該這樣做:

$get_monthly_payments_lastmonth=AppPayment::where('payment_annum','=','monthly')
        ->whereDate('created_at', '>', Carbon::now()->subMonth())
        ->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM