add crud general settings

This commit is contained in:
arifal hidayat
2025-01-28 05:22:50 +07:00
parent 14f68c0add
commit 538cdb87ae
12 changed files with 387 additions and 61 deletions

View File

@@ -3,15 +3,110 @@
namespace App\Http\Controllers\Settings;
use App\Http\Controllers\Controller;
use App\Http\Requests\GlobalSettingRequest;
use App\Http\Requests\UpdateGlobalSettingRequest;
use App\Models\GlobalSetting;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class SettingsController extends Controller
{
public function index(){
/**
* Display a listing of the resource.
*/
public function index()
{
return view('settings.general.index');
}
public function create(){
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('settings.general.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(GlobalSettingRequest $request)
{
try{
DB::beginTransaction();
GlobalSetting::create($request->validated());
DB::commit();
return redirect()->route('general.index')->with('success', 'Data saved successfully.');
}catch(Exception $e){
DB::rollBack();
return redirect()->back()
->withInput()
->with('error', 'Something went wrong while saving data. ' . $e->getMessage());
}
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$data = GlobalSetting::find($id);
if(!$data){
return redirect()->route('general.index')->with('error', 'Invalid id');
}
return view('settings.general.show', compact('data'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$data = GlobalSetting::find($id);
if(!$data){
return redirect()->route('general.index')->with('error', 'Invalid id');
}
return view('settings.general.edit', compact('data'));
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateGlobalSettingRequest $request, string $id)
{
try{
DB::beginTransaction();
$data = GlobalSetting::findOrFail($id);
$data->update($request->validated());
DB::commit();
return redirect()->route('general.index')->with('success', 'Data updated successfully.');
}catch(Exception $e){
DB::rollBack();
return redirect()->back()
->withInput()
->with('error', 'Something went wrong while updating data. '. $e->getMessage());
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
try{
DB::beginTransaction();
$data = GlobalSetting::findOrFail($id);
$data->delete();
DB::commit();
return response()->json(['success' => true, 'message' => 'Item deleted successfully.']);
}catch(Exception $e){
DB::rollBack();
Log::error($e->getMessage());
return response()->json(['success' => false, 'message' => 'Failed to delete item.'], 500);
}
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UpdateGlobalSettingRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$id = $this->route('general');
return [
'key' => 'required|unique:global_settings,key,' . $id,
'value' => 'required',
'type' => 'nullable',
'description' => 'nullable'
];
}
}