Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shiva's avatar
Level 5

Getting my query to look at the year

I'm trying to have my query do this. Grab all the orders where the order_date equals to the year and where the user_id equals users id. I have managed to get the year buy I'm struggling to get it check the database for all orders that has that year.

My code

public function sortOrders(Request $request)
{
    $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
    $contacts = Contact::all();
    $user_id = Auth::user()->id;

    if($request->sort_orders === '2019')
    {
  
        $order_list = Order::where('user_id', $user_id)->get();

        foreach($order_list as $order)
        {
            $year = Carbon::parse($order->order_date)->year;
        }
        
        $orders = Order::where('order_date', $year)->where('user_id', $user_id)->orderBy('order_date', 'desc')->get();
        $orders->transform(function($order, $key){
                $order->cart = unserialize($order->cart);
                return $order;
            });
    }

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

Please or to participate in this conversation.