Nikki's avatar
Level 1

Not sure how to get 3 months

I'm trying to create a sort function where if it's selected it will display the necessary amount of orders. For example if the user selects to display orders from the lat 3 months then that needs to be displayed.

At the moment I'm stuck at the section where I say it's 3 monts

public function trackOrders()
    {
        $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
        $contacts = Contact::all();

        $orders = Auth::user()->orders->sortByDesc('order_date');
        $orders->transform(function($order, $key){
                    $order->cart = unserialize($order->cart);
                    return $order;
                });

        $from = Carbon\Carbon::now('+2:00');

    // This is where I'm stuck
        $to = 

        $three_months = Order:: where($from, '>', $to);

        return view('public.users.track-orders', compact('menus_child', 'contacts', 'orders', 'order_item'));
    }
0 likes
7 replies
tykus's avatar

This should give you the basic idea:

$from = now();
$to = $from->copy()->addMonths(3);

Order::whereBetween('created_at', [$from, $to])->get();
esmaill23's avatar

this is your slove :

$from = now();
$to = now() + 3*30*24*60*60;
gorakhyadav's avatar

Replace with this .

  $from = now();
  $to = $from->copy()->addMonths(3);
  $three_months = Order::whereBetween('created_at', [$from, $to])->get();
Nikki's avatar
Level 1

I'm getting nothing when I dd($three_months);

gorakhyadav's avatar

Try with that.

         $from= Carbon::now()->startOfMonth()->subMonth(3);
         $to= Carbon::now()->startOfMonth(); 
         $three_months = Order::whereBetween('created_at', [$from, $to])->get();

Please or to participate in this conversation.