简体   繁体   中英

how to get latest date record from array in laravel

Hello all i am using laravel elequont i want to fetch record of latest date from array can someone help how can i achieve this. mention below is my array

[
{
            "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"
        },            
]

this is my query

$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();

I want to fetch the record of latest month like here i want to fetch sept record only i am using laravel any help would be appreciated

You can modify your query like so:


$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();

the MY format should result in Sep 2021 (at the time of writing).

A generic approach based on your billing span would be:


$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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM