John Davidson

php - Optimize eloquent queries

0 comments
Message:


I have to get stats from database like this


$stats = [
'products_count' => Product::where('status', 1)->count(),
'last_year_products_count' => Product::whereYear('created_at', now()->subYear()->year)->count(),
'last_year_order_items_count' => OrderItem::whereYear('created_at', now()->subYear()->year)->sum('quantity'),
'order_item_unique_count' => OrderItem::distinct('product_id')->count(),
'last_year_order_item_unique_count' => OrderItem::whereYear('created_at', now()->subYear()->year)->distinct('product_id')->count(),
'order_products_avg' => OrderItem::avg('quantity'),
'last_year_order_products_avg' => OrderItem::whereYear('created_at', now()->subYear()->year)->avg('quantity'),
];

I want optimize sql queries most as possible



My Answer:
Product::count(),
'users_count' => User::count(),
'orders_count' => Order::count(),
'total_revenue' => Order::sum('revenue')
];

Rate this post

5 of 5 based on 2445 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us