Level 1
You should check for 'whereYear' method for checking records that has that year (https://laravel.com/docs/5.8/queries)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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'));
}
@DRAGONEYES96 - Thank you so much.
Please or to participate in this conversation.