RemarkTitleController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\RemarkTitle;
  4. use Illuminate\Http\Request;
  5. class RemarkTitleController extends Controller
  6. {
  7. protected $redirect_index = '/admin/RemarkTitle/index';
  8. protected $view_path = 'admin.remark-titles.';
  9. protected $pre_uri = '/admin/RemarkTitle/';
  10. protected $model_name = '评价题目';
  11. protected $model;
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->model = new RemarkTitle();
  16. }
  17. public function index(Request $request)
  18. {
  19. $list = $this->model->where('id', '>', 0)->orderBy('updated_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 create(Request $request)
  29. {
  30. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  31. return view($this->view_path . 'create', compact('pre_uri', 'model_name', 'model'));
  32. }
  33. public function store(Request $request)
  34. {
  35. if(!$request->isMethod('POST')) {
  36. return $this->showWarning('访问错误');
  37. }
  38. if(empty($request->input('data')) || !is_array($request->input('data'))) {
  39. return $this->showWarning('数据错误');
  40. }
  41. $res = $this->model->create($request->input('data'));
  42. if(!$res) {
  43. return $this->showWarning('数据库保存失败!');
  44. }
  45. return $this->showMessage('操作成功', $this->redirect_index);
  46. }
  47. public function edit(Request $request)
  48. {
  49. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  50. return $this->showWarning('数据错误!');
  51. }
  52. list($pre_uri, $model_name, $model) = array($this->pre_uri, $this->model_name, $this->model);
  53. return view($this->view_path . 'edit', compact('item','pre_uri', 'model_name', 'model'));
  54. }
  55. public function update(Request $request)
  56. {
  57. if(!$request->isMethod('POST')) {
  58. return $this->showWarning('访问错误');
  59. }
  60. if(empty($request->input('id')) || empty($request->input('data')) || !is_array($request->input('data'))) {
  61. return $this->showWarning('数据错误');
  62. }
  63. $res = $this->model->where('id', $request->input('id'))->update($request->input('data'));
  64. if(!$res) {
  65. return $this->showWarning('数据库保存失败!');
  66. }
  67. return $this->showMessage('操作成功', $this->redirect_index);
  68. }
  69. public function delete(Request $request)
  70. {
  71. if(!$request->isMethod('POST')) {
  72. return $this->showWarning('访问错误');
  73. }
  74. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) {
  75. return $this->showWarning('访问错误');
  76. }
  77. $res = $item->delete();
  78. if(!$res) {
  79. return $this->showWarning('数据库删除失败');
  80. }
  81. return $this->showMessage('操作成功');
  82. }
  83. }