Bilal Arshad
Bilal Arshad

Reputation: 571

How to get data which has expiry date nearest to current date?

I have PurchaseOrderProduct Model from which I am getting data like that

PurchaseOrderProduct::with('products')
                     ->with('warehouse_locations')
                     ->with('productStatuses')
                     ->with('purchase_orders')
                     ->where('product_id', $request->product_id)
                     ->where('free_qty', '>=', 1)
                     ->get();

Now in this table, I have the column expiry_date of type date i want to fetch data orderBy expiry_date nearest to the current date .

Means purchase_order_product whose expiry date is nearest to current date should come first.

Upvotes: 3

Views: 3014

Answers (1)

Yudiz Solutions
Yudiz Solutions

Reputation: 4459

$product = PurchaseOrderProduct::with('products')
           ->with('warehouse_locations')
           ->with('productStatuses')
           ->with('purchase_orders')
           ->where('product_id', $request->product_id)
           ->where('free_qty', '>=', 1)
           ->whereDate('expiry_date', '>', Carbon::now())
           ->orderBy('expiry_date','asc')
           ->get();

Upvotes: 5

Related Questions