AdminController.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Category;
  5. use App\Models\Product;
  6. use Artisan;
  7. use Cache;
  8. use CoreComponentRepository;
  9. class AdminController extends Controller
  10. {
  11. /**
  12. * Show the admin dashboard.
  13. *
  14. * @return \Illuminate\Http\Response
  15. */
  16. public function admin_dashboard(Request $request)
  17. {
  18. CoreComponentRepository::initializeCache();
  19. $root_categories = Category::where('level', 0)->get();
  20. $cached_graph_data = Cache::remember('cached_graph_data', 86400, function() use ($root_categories){
  21. $num_of_sale_data = null;
  22. $qty_data = null;
  23. foreach ($root_categories as $key => $category){
  24. $category_ids = \App\Utility\CategoryUtility::children_ids($category->id);
  25. $category_ids[] = $category->id;
  26. $products = Product::with('stocks')->whereIn('category_id', $category_ids)->get();
  27. $qty = 0;
  28. $sale = 0;
  29. foreach ($products as $key => $product) {
  30. $sale += $product->num_of_sale;
  31. foreach ($product->stocks as $key => $stock) {
  32. $qty += $stock->qty;
  33. }
  34. }
  35. $qty_data .= $qty.',';
  36. $num_of_sale_data .= $sale.',';
  37. }
  38. $item['num_of_sale_data'] = $num_of_sale_data;
  39. $item['qty_data'] = $qty_data;
  40. return $item;
  41. });
  42. return view('backend.dashboard', compact('root_categories', 'cached_graph_data'));
  43. }
  44. function clearCache(Request $request)
  45. {
  46. Artisan::call('cache:clear');
  47. flash(translate('Cache cleared successfully'))->success();
  48. return back();
  49. }
  50. }