[英]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.