ProductController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Auth;
  4. use Request;
  5. use Validator;
  6. use App\Models\product;
  7. use App\Http\Requests\ProductRequest;
  8. class ProductController extends BaseController
  9. {
  10. /* 视图文件相对路径 */
  11. protected $view = 'admin.product.list';
  12. protected $view_addupd = 'admin.product.addupd';
  13. protected $menu_name = 'product';
  14. public function __construct()
  15. {
  16. parent::__construct($this->menu_name);
  17. }
  18. // 获取所属菜单选项
  19. public function getBelongMenus()
  20. {
  21. $pro_sort1 = $this->menu_home->where('ename', 'pro')
  22. ->first()
  23. ->sort1;
  24. $menus_sort1 = [$pro_sort1];
  25. return $this->getSomeSecondMenus($menus_sort1);
  26. }
  27. /* 产品管理 */
  28. // 页面展示
  29. public function index()
  30. {
  31. $products = Product::where([])
  32. ->orderBy('sort')
  33. ->paginate(10);
  34. $view_para = array_add($this->view_para, 'products', $products);
  35. $view_para = array_add($view_para, 'menu_home', $this->menu_home);
  36. return view($this->view, $view_para);
  37. }
  38. /* 添加 */
  39. // 页面展示
  40. public function addView()
  41. {
  42. $view_para = array_add($this->view_para, 'menus', $this->getBelongMenus());
  43. return view($this->view_addupd, $view_para);
  44. }
  45. // 表单处理
  46. public function addPost(ProductRequest $request)
  47. {
  48. $image = Request::file('image');
  49. if (!$image) {
  50. return redirect()->back()->with('errors_image', '图片不可为空')->withInput();
  51. }
  52. $errors_image = $this->imageValidator($image);
  53. if ($errors_image) {
  54. return redirect()->back()->with('errors_image', $errors_image)->withInput();
  55. }
  56. $path = $this->imageUpload($this->image_name, $image, $this->image_path);
  57. $data = Request::except(['_token', 'image']);
  58. $data['image'] = $path;
  59. $data['belong'] = implode(',', Request::input('belong'));
  60. product::create($data);
  61. return redirect()->route($this->menu_name)->with('success', '添加成功');
  62. }
  63. /* 编辑 */
  64. // 页面展示
  65. public function updView($id)
  66. {
  67. $product = product::find($id);
  68. if (!$product) {
  69. return redirect()->route($this->menu_name)->with('error', '找不到该课程');
  70. }
  71. $view_para = array_add($this->view_para, 'product', $product);
  72. $view_para = array_add($view_para, 'menus', $this->getBelongMenus());
  73. return view($this->view_addupd, $view_para);
  74. }
  75. // 表单处理
  76. public function updPost($id, productRequest $request)
  77. {
  78. $product = product::find($id);
  79. if (!$product) {
  80. return redirect()->route($this->menu_name)->with('error', '找不到该产品');
  81. }
  82. $image = Request::file('image');
  83. if ($image) {
  84. $errors_image = $this->imageValidator($image);
  85. if ($errors_image) {
  86. return redirect()->back()->with('errors_image', $errors_image)->withInput();
  87. }
  88. $path = $this->imageUpload($this->image_name, $image, $this->image_path);
  89. $product->image = $path;
  90. }
  91. $product = Request::except(['_token', 'image']);
  92. $product['belong'] = implode(',', Request::input('belong'));
  93. product::where('id', $id)
  94. ->update($product);
  95. return redirect()->route($this->menu_name)->with('success', '编辑成功');
  96. }
  97. /* 删除 */
  98. public function del($id)
  99. {
  100. product::destroy($id);
  101. return redirect()->route($this->menu_name)->with('success', '删除成功');
  102. }
  103. }