|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use App\Http\Requests; |
| 6 | +use App\Http\Controllers\Controller; |
| 7 | + |
| 8 | +use App\Setting; |
| 9 | +use Illuminate\Http\Request; |
| 10 | + |
| 11 | +class SettingsController extends Controller |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Display a listing of the resource. |
| 15 | + * |
| 16 | + * @return \Illuminate\View\View |
| 17 | + */ |
| 18 | + public function index(Request $request) |
| 19 | + { |
| 20 | + $keyword = $request->get('search'); |
| 21 | + $perPage = 25; |
| 22 | + |
| 23 | + if (!empty($keyword)) { |
| 24 | + $settings = Setting::where('key', 'LIKE', "%$keyword%") |
| 25 | + ->orWhere('value', 'LIKE', "%$keyword%") |
| 26 | + ->orderBy('key')->paginate($perPage); |
| 27 | + } else { |
| 28 | + $settings = Setting::orderBy('key')->paginate($perPage); |
| 29 | + } |
| 30 | + |
| 31 | + return view('admin.settings.index', compact('settings')); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Show the form for creating a new resource. |
| 36 | + * |
| 37 | + * @return \Illuminate\View\View |
| 38 | + */ |
| 39 | + public function create() |
| 40 | + { |
| 41 | + return view('admin.settings.create'); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Store a newly created resource in storage. |
| 46 | + * |
| 47 | + * @param \Illuminate\Http\Request $request |
| 48 | + * |
| 49 | + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
| 50 | + */ |
| 51 | + public function store(Request $request) |
| 52 | + { |
| 53 | + $this->validate( |
| 54 | + $request, |
| 55 | + [ |
| 56 | + 'key' => 'required|string|unique:settings', |
| 57 | + 'value' => 'required' |
| 58 | + ] |
| 59 | + ); |
| 60 | + |
| 61 | + $requestData = $request->all(); |
| 62 | + |
| 63 | + Setting::create($requestData); |
| 64 | + |
| 65 | + return redirect('admin/settings')->with('flash_message', 'Setting added!'); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Display the specified resource. |
| 70 | + * |
| 71 | + * @param int $id |
| 72 | + * |
| 73 | + * @return \Illuminate\View\View |
| 74 | + */ |
| 75 | + public function show($id) |
| 76 | + { |
| 77 | + $setting = Setting::findOrFail($id); |
| 78 | + |
| 79 | + return view('admin.settings.show', compact('setting')); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Show the form for editing the specified resource. |
| 84 | + * |
| 85 | + * @param int $id |
| 86 | + * |
| 87 | + * @return \Illuminate\View\View |
| 88 | + */ |
| 89 | + public function edit($id) |
| 90 | + { |
| 91 | + $setting = Setting::findOrFail($id); |
| 92 | + |
| 93 | + return view('admin.settings.edit', compact('setting')); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Update the specified resource in storage. |
| 98 | + * |
| 99 | + * @param \Illuminate\Http\Request $request |
| 100 | + * @param int $id |
| 101 | + * |
| 102 | + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
| 103 | + */ |
| 104 | + public function update(Request $request, $id) |
| 105 | + { |
| 106 | + $this->validate( |
| 107 | + $request, |
| 108 | + [ |
| 109 | + 'key' => 'required|string|unique:settings,key,' . $id, |
| 110 | + 'value' => 'required' |
| 111 | + ] |
| 112 | + ); |
| 113 | + $requestData = $request->all(); |
| 114 | + |
| 115 | + $setting = Setting::findOrFail($id); |
| 116 | + $setting->update($requestData); |
| 117 | + |
| 118 | + return redirect('admin/settings')->with('flash_message', 'Setting updated!'); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Remove the specified resource from storage. |
| 123 | + * |
| 124 | + * @param int $id |
| 125 | + * |
| 126 | + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
| 127 | + */ |
| 128 | + public function destroy($id) |
| 129 | + { |
| 130 | + Setting::destroy($id); |
| 131 | + |
| 132 | + return redirect('admin/settings')->with('flash_message', 'Setting deleted!'); |
| 133 | + } |
| 134 | +} |
0 commit comments