OrderController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Order;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. class OrderController extends Controller
  7. {
  8. protected $redirect_index = '/admin/Order/index';
  9. protected $view_path = 'admin.orders.';
  10. protected $pre_uri = '/admin/Order/';
  11. protected $model_name = '订单';
  12. protected $model;
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->model = new Order();
  17. }
  18. public function index(Request $request)
  19. {
  20. $list = $this->model->where('id', '>', 0);
  21. if(!empty($request->input('pay_status')) && in_array($request->input('pay_status'), [1, 2, 3, 4, 5])) {
  22. $list = $list->where('pay_status', $request->input('pay_status'));
  23. }
  24. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  25. $keyword = '%' . trim($request->input('keyword')) . '%';
  26. $list = $list->whereHas('student', function ($query) use($keyword) {
  27. $query->where('name', 'like', $keyword);
  28. });
  29. }
  30. if(!empty($request->input('start_time'))) {
  31. $start_time = Carbon::createFromTimestamp(strtotime($request->input('start_time')))->toDateTimeString();
  32. } else {
  33. $start_time = Carbon::now()->subYears(10)->toDateTimeString();
  34. }
  35. if(!empty($request->input('end_time'))) {
  36. $end_time = Carbon::createFromTimestamp(strtotime($request->input('end_time')))->addDay()->toDateTimeString();
  37. } else {
  38. $end_time = Carbon::now()->addYears(10)->toDateTimeString();
  39. }
  40. $list = $list->where([
  41. ['created_at', '>=', $start_time],
  42. ['created_at', '<', $end_time],
  43. ]);
  44. $list = $list->paginate()->withPath($this->getPaginateUrl());
  45. list($pre_uri, $model, $model_name) = array($this->pre_uri, $this->model, $this->model_name);
  46. $seven_days_ago = Carbon::now()->subDays(7)->toDateTimeString();
  47. $one_month_ago = Carbon::now()->subMonth()->toDateTimeString();
  48. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model', 'model_name', 'seven_days_ago', 'one_month_ago'));
  49. }
  50. public function delete(Request $request)
  51. {
  52. if(!$request->isMethod('POST')) {
  53. return $this->showWarning('访问错误');
  54. }
  55. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  56. return $this->showWarning('访问错误');
  57. }
  58. $res = $item->delete();
  59. if(!$res) {
  60. return $this->showWarning('数据库删除失败');
  61. }
  62. return $this->showMessage('操作成功');
  63. }
  64. }