create crud data settings
This commit is contained in:
104
app/Http/Controllers/Api/DataSettingController.php
Normal file
104
app/Http/Controllers/Api/DataSettingController.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\DataSettingRequest;
|
||||
use App\Http\Resources\DataSettingResource;
|
||||
use App\Models\DataSetting;
|
||||
use App\Traits\GlobalApiResponse;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DataSettingController extends Controller
|
||||
{
|
||||
use GlobalApiResponse;
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
try {
|
||||
$query = DataSetting::query()->orderBy('id', 'desc');
|
||||
if ($request->has("search") && !empty($request->get("search"))) {
|
||||
$query = $query->where("key", $request->get("search"));
|
||||
}
|
||||
|
||||
return DataSettingResource::collection($query->paginate());
|
||||
} catch (Exception $e) {
|
||||
return $this->resError($e->getMessage(), $e->getTrace());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(DataSettingRequest $request)
|
||||
{
|
||||
try {
|
||||
$data = DataSetting::create($request->validated());
|
||||
$result = [
|
||||
"success" => true,
|
||||
"message" => "Data Setting created successfully",
|
||||
"data" => new DataSettingResource($data)
|
||||
];
|
||||
return $this->resSuccess($result);
|
||||
} catch (Exception $e) {
|
||||
return $this->resError($e->getMessage(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
try {
|
||||
$setting = DataSetting::findOrFail($id);
|
||||
$result = [
|
||||
"setting" => true,
|
||||
"message" => "Data setting successfully",
|
||||
"data" => new DataSettingResource($setting)
|
||||
];
|
||||
return $this->resSuccess($result);
|
||||
} catch (Exception $e) {
|
||||
return $this->resError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(DataSettingRequest $request, string $id)
|
||||
{
|
||||
try {
|
||||
$data = DataSetting::findOrFail($id);
|
||||
$data->update($request->validated());
|
||||
$result = [
|
||||
"success" => true,
|
||||
"message" => "Data Setting updated successfully"
|
||||
];
|
||||
return $this->resSuccess($result);
|
||||
} catch (Exception $e) {
|
||||
return $this->resError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
try {
|
||||
$setting = DataSetting::findOrFail($id);
|
||||
$setting->delete();
|
||||
$result = [
|
||||
"success" => true,
|
||||
"message" => "Data Setting deleted successfully"
|
||||
];
|
||||
return $this->resSuccess($result);
|
||||
} catch (Exception $e) {
|
||||
return $this->resError($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
106
app/Http/Controllers/DataSettingController.php
Normal file
106
app/Http/Controllers/DataSettingController.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\DataSettingRequest;
|
||||
use App\Models\DataSetting;
|
||||
use Exception;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class DataSettingController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view("data-settings.index");
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view("data-settings.create");
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(DataSettingRequest $request)
|
||||
{
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
DataSetting::create($request->validated());
|
||||
DB::commit();
|
||||
return redirect()->route("data-settings.index")->with("success","Successfully created");
|
||||
}catch(Exception $ex){
|
||||
DB::rollBack();
|
||||
return redirect()->back()
|
||||
->withInput()
|
||||
->with('error', 'Something went wrong while saving data. ' . $ex->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(DataSetting $dataSetting)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
{
|
||||
try{
|
||||
$data = DataSetting::findOrFail($id);
|
||||
if(empty($data)){
|
||||
return redirect()->route('data-settings.index')->with('error', 'Invalid id');
|
||||
}
|
||||
return view("data-settings.edit", compact("data"));
|
||||
}catch(Exception $ex){
|
||||
return redirect()->route("data-settings.index")->with("error", "Invalid id");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(DataSettingRequest $request,string $id)
|
||||
{
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
$data = DataSetting::findOrFail($id);
|
||||
$data->update($request->validated());
|
||||
DB::commit();
|
||||
return redirect()->route("data-settings.index")->with("success","Successfully updated");
|
||||
}catch(Exception $ex){
|
||||
DB::rollBack();
|
||||
return redirect()->back()
|
||||
->withInput()
|
||||
->with('error', 'Something went wrong while saving data. ' . $ex->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
try{
|
||||
DB::beginTransaction();
|
||||
DataSetting::findOrFail($id)->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,30 +15,4 @@ class RoutingController extends Controller
|
||||
return redirect('auth.signin');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a view based on first route param
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function root(Request $request, $first)
|
||||
{
|
||||
return view($first);
|
||||
}
|
||||
|
||||
/**
|
||||
* second level route
|
||||
*/
|
||||
public function secondLevel(Request $request, $first, $second)
|
||||
{
|
||||
return view($first . '.' . $second);
|
||||
}
|
||||
|
||||
/**
|
||||
* third level route
|
||||
*/
|
||||
public function thirdLevel(Request $request, $first, $second, $third)
|
||||
{
|
||||
return view($first . '.' . $second . '.' . $third);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user