This commit is contained in:
sayaliparab
2024-07-02 15:24:49 +05:30
parent c87448dea0
commit 3003e10003
5 changed files with 439 additions and 126 deletions

View File

@@ -18,9 +18,11 @@ class ManageLocationController extends Controller
Created at : 07 June 2024
Use : To get the page.
*/
public function index()
{
$location = ManageState::with('timeInterval')->orderBy('id', 'asc')->get();
// return $location;
return view('Admin.pages.manage_states.manage_states', compact('location'));
}
/*
@@ -122,29 +124,56 @@ class ManageLocationController extends Controller
Use : To update location.
*/
public function update(Request $request)
{
try {
DB::beginTransaction();
// Check if the location name already exists, excluding the current location
if (ManageState::where('name', $request->location_name)->where('id', '!=', $request->id)->exists()) {
return response()->json(['success' => false, 'error' => 'Location name already exists', 'status' => 422]);
}
$location_data = ManageState::find($request->id);
$location_data->name = $request->location_name;
$location_data->save();
public function update_location(Request $request)
{
try {
DB::beginTransaction();
DB::commit();
return response()->json(['success' => true, 'message' => __('success.update_data'), 'status' => 200]);
} catch (Exception $e) {
DB::rollBack();
Log::error("Failed to update location: " . $e->getMessage());
return response()->json(['success' => false, 'error' => __('auth.something_went_wrong'), 'status' => 500]);
// Find the location data
$location_data = ManageState::find($request->id);
if (!$location_data) {
return response()->json(['success' => false, 'error' => 'Location not found', 'status' => 404]);
}
}
// Update the location data
$location_data->name = $request->location_name;
$location_data->save();
// Update or create the TimeInterval data
$timeInterval = TimeInterval::where('manage_state_xid', $request->input('id'))->first();
if ($timeInterval) {
// Update existing record
$timeInterval->update([
'manage_state_xid' => $request->input('id'),
'time_hours' => $request->input('time_hours'),
'time_interval' => $request->input('time_interval'),
'quantity' => $request->input('quantity'),
]);
} else {
// Create new record
TimeInterval::create([
'manage_state_xid' => $request->input('id'),
'time_hours' => $request->input('time_hours'),
'time_interval' => $request->input('time_interval'),
'quantity' => $request->input('quantity'),
]);
}
DB::commit();
return response()->json(['success' => true, 'message' => __('success.update_data'), 'status' => 200]);
} catch (Exception $e) {
DB::rollBack();
Log::error("Failed to update location: " . $e->getMessage());
return response()->json(['success' => false, 'error' => __('auth.something_went_wrong'), 'status' => 500]);
}
}
}