SettingController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers\Admin\Base;
  3. use App\Http\Controllers\Admin\Controller;
  4. use App\Models\BaseSettingsModel;
  5. class SettingController extends Controller
  6. {
  7. /**
  8. * Display a listing of the resource.
  9. *
  10. * @return \Illuminate\Http\Response
  11. */
  12. public function index()
  13. {
  14. return view('admin.base.setting.index', [
  15. 'items' => BaseSettingsModel::where('category', '<>', 'category')->orderBy('sort', 'asc')->orderBy('key', 'asc')->get(),
  16. 'categories' => BaseSettingsModel::where('category', '=', 'category')->get(),
  17. ]);
  18. }
  19. /**
  20. * Show the form for creating a new resource.
  21. *
  22. * @return \Illuminate\Http\Response
  23. */
  24. public function create()
  25. {
  26. return view('admin.setting.create', [
  27. 'categories' => Setting::where('category', '=', 'category')->get(),
  28. ]);
  29. }
  30. /**
  31. * Store a newly created resource in storage.
  32. *
  33. * @param \Illuminate\Http\Request $request
  34. * @return \Illuminate\Http\Response
  35. */
  36. public function store(SettingRequest $request)
  37. {
  38. if ($data = $request->all()) {
  39. if (Setting::where(['category' => $data['category'], 'code' => $data['code']])->exists()) {
  40. return back()->with('errors', ['该类别下已有相同键存在!']);
  41. }
  42. $data['sort'] = empty($request->sort) ? 0 : $request->sort;
  43. $item = Setting::create($data);
  44. if ($item) {
  45. Toastr::success('商户添加成功!');
  46. return redirect(route('admin.setting.index'));
  47. } else {
  48. Toastr::success('商户添加失败!');
  49. return back()->with('errors', ['商户存储失败!']);
  50. }
  51. }
  52. }
  53. /**
  54. * Show the form for editing the specified resource.
  55. *
  56. * @param int $id
  57. * @return \Illuminate\Http\Response
  58. */
  59. public function edit($id)
  60. {
  61. Breadcrumbs::register('admin-setting-edit', function ($breadcrumbs) use ($id) {
  62. $breadcrumbs->parent('admin-setting');
  63. $breadcrumbs->push('编辑配置', route('admin.setting.edit', ['id' => $id]));
  64. });
  65. $item = Setting::find($id);
  66. if(!$item) {
  67. Toastr::error('配置不存在');
  68. return redirect(route('admin.setting.index'));
  69. }
  70. return view('admin.setting.edit', [
  71. 'item' => $item,
  72. 'categories' => Setting::where('category', '=', 'category')->get(),
  73. ]);
  74. }
  75. /**
  76. * Update the specified resource in storage.
  77. *
  78. * @param \Illuminate\Http\Request $request
  79. * @param int $id
  80. * @return \Illuminate\Http\Response
  81. */
  82. public function update(SettingRequest $request, $id)
  83. {
  84. $item = Setting::find($id);
  85. if(!$item) {
  86. return $this->showMessage('配置不存在');
  87. Toastr::error('配置不存在');
  88. return redirect(route('admin.setting.index'));
  89. }
  90. if ($data = $request->all()) {
  91. $oldItem = Setting::where(['category' => $data['category'], 'code' => $data['code']])->first();
  92. if ($oldItem !== null && $oldItem->id != $id) {
  93. return back()->with('errors', ['该类别下已有相同键存在!']);
  94. }
  95. $result = $item->update($data);
  96. if ($result) {
  97. Toastr::success('配置编辑成功!');
  98. return redirect()->route('admin.setting.index');
  99. } else {
  100. Toastr::error('配置编辑失败!');
  101. return back()->with('errors', ['配置编辑失败!']);
  102. }
  103. }
  104. }
  105. /**
  106. * Remove the specified resource from storage.
  107. *
  108. * @param int $id
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function destroy($id)
  112. {
  113. $app = Setting::find($id);
  114. if($app->delete()){
  115. return $this->showMessage('配置删除成功');
  116. }else{
  117. return $this->showWarning('配置删除成功');
  118. }
  119. return redirect(route('admin.setting.index'));
  120. }
  121. }