FormDataController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\FormData;
  4. use Illuminate\Http\Request;
  5. class FormDataController extends Controller
  6. {
  7. protected $redirect_index = '/admin/FormData/index';
  8. protected $view_path = 'admin.form-data.';
  9. protected $pre_uri = '/admin/FormData/';
  10. protected $model_name = '表单数据';
  11. protected $model;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->model = new FormData();
  16. }
  17. public function index(Request $request)
  18. {
  19. $list = $this->model->where('id', '>', 0)->orderBy('created_at', 'desc');
  20. if(!empty($request->input('keyword')) && !empty(trim($request->input('keyword')))) {
  21. $keyword = '%' . trim($request->input('keyword')) . '%';
  22. $list = $list->where('name', 'like', $keyword);
  23. }
  24. $list = $list->paginate()->withPath($this->getPaginateUrl());
  25. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  26. return view($this->view_path . 'index', compact('list', 'pre_uri', 'model_name'));
  27. }
  28. public function show(Request $request)
  29. {
  30. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  31. return $this->showWarning('参数错误');
  32. }
  33. $item->status = 2;
  34. $item->save();
  35. list($pre_uri, $model_name) = array($this->pre_uri, $this->model_name);
  36. return view($this->view_path . 'show', compact('list', 'pre_uri', 'model_name', 'item'));
  37. }
  38. public function delete(Request $request)
  39. {
  40. if(!$request->isMethod('POST')) {
  41. return $this->showWarning('访问错误');
  42. }
  43. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  44. return $this->showWarning('访问错误');
  45. }
  46. $res = $item->delete();
  47. if(!$res) {
  48. return $this->showWarning('数据库删除失败');
  49. }
  50. return $this->showMessage('操作成功');
  51. }
  52. }