簡體   English   中英

如何從laravel的數組中獲取最新的日期記錄

[英]how to get latest date record from array in laravel

大家好,我正在使用 laravel elequont 我想從數組中獲取最新日期的記錄有人可以幫助我如何實現這一點。 下面提到的是我的陣列

[
{
            "id": 266,
            "customer_id": 19,
            "bill_no": 239,
            "bill_period": "monthly",
            "from_date": "2021-07-01",
            "to_date": "2021-07-31",
            "month": "Jul 2021",
            "total_litres": "617",
            "amount": "42573",
            "previous_balance": "177140",
            "total_amount": "219713",
            "amount_paid": "0",
            "pending_amount": "219713",
            "adjusted": "0",
            "status": 1,
            "created_at": "2021-07-31 02:33:54",
            "updated_at": null,
            "customer_name": "amit farm"
        },
        {
            "id": 267,
            "customer_id": 20,
            "bill_no": 240,
            "bill_period": "monthly",
            "from_date": "2021-07-01",
            "to_date": "2021-07-31",
            "month": "Jul 2021",
            "total_litres": "1240",
            "amount": "85560",
            "previous_balance": "265995",
            "total_amount": "351555",
            "amount_paid": "0",
            "pending_amount": "351555",
            "adjusted": "0",
            "status": 1,
            "created_at": "2021-07-31 02:33:54",
            "updated_at": null,
            "customer_name": "rahul farm"
        },                
        {
            "id": 321,
            "customer_id": 19,
            "bill_no": 294,
            "bill_period": "monthly",
            "from_date": "2021-08-01",
            "to_date": "2021-08-31",
            "month": "Aug 2021",
            "total_litres": "620",
            "amount": "42780",
            "previous_balance": "219713",
            "total_amount": "262493",
            "amount_paid": "0",
            "pending_amount": "262493",
            "adjusted": "0",
            "status": 1,
            "created_at": "2021-09-01 03:39:26",
            "updated_at": null,
            "customer_name": "amit farm"
        },
        {
            "id": 322,
            "customer_id": 20,
            "bill_no": 295,
            "bill_period": "monthly",
            "from_date": "2021-08-01",
            "to_date": "2021-08-31",
            "month": "Aug 2021",
            "total_litres": "1245",
            "amount": "85905",
            "previous_balance": "351555",
            "total_amount": "437460",
            "amount_paid": "0",
            "pending_amount": "437460",
            "adjusted": "0",
            "status": 1,
            "created_at": "2021-09-01 03:39:26",
            "updated_at": null,
            "customer_name": "rahul farm"
        },        
        {
            "id": 368,
            "customer_id": 19,
            "bill_no": 341,
            "bill_period": "monthly",
            "from_date": "2021-09-01",
            "to_date": "2021-09-30",
            "month": "Sep 2021",
            "total_litres": "600",
            "amount": "41400",
            "previous_balance": "262493",
            "total_amount": "303893",
            "amount_paid": "0",
            "pending_amount": "95378",
            "adjusted": "0",
            "status": 1,
            "created_at": "2021-10-01 02:26:46",
            "updated_at": null,
            "customer_name": "amit DAIRY"
        },
        {
            "id": 369,
            "customer_id": 20,
            "bill_no": 342,
            "bill_period": "monthly",
            "from_date": "2021-09-01",
            "to_date": "2021-09-30",
            "month": "Sep 2021",
            "total_litres": "1200",
            "amount": "82800",
            "previous_balance": "437460",
            "total_amount": "520260",
            "amount_paid": "0",
            "pending_amount": "294692",
            "adjusted": "0",
            "status": 1,
            "created_at": "2021-10-01 02:26:46",
            "updated_at": null,
            "customer_name": "rahul farm"
        },            
]

這是我的查詢

$wdata= DB::table('weekly_billing')
                ->Join('customers', 'weekly_billing.customer_id', '=', 'customers.id')
                ->select('weekly_billing.*','customers.customer_name','customers.bill_period')
                ->where('weekly_billing.bill_period','monthly')
                ->get();

我想獲取最近一個月的記錄,就像這里我只想獲取 sept 記錄,我正在使用 laravel 任何幫助將不勝感激

您可以像這樣修改查詢:


$wdata= DB::table('weekly_billing')
    ->join('customers', 'weekly_billing.customer_id', '=', 'customers.id')
    ->select('weekly_billing.*','customers.customer_name','customers.bill_period')
    ->where('weekly_billing.bill_period','monthly')
    ->where('month', Carbon::now()->format('M Y'))
    ->get();

MY格式的結果應該是Sep 2021Sep 2021 (在撰寫本文時)。

基於您的計費范圍的通用方法是:


$wdata= DB::table('weekly_billing')
    ->join('customers', 'weekly_billing.customer_id', '=', 'customers.id')
    ->select('weekly_billing.*','customers.customer_name','customers.bill_period')
    ->whereRaw(Carbon::now()->format('Y m d').' BETWEEN from_date AND to_date')
    ->get();

暫無
暫無

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

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