xxxxxxxxxx
try {
// Perform API request using Laravel's HTTP client or any other API client library
$response = Http::get('https://api.example.com/endpoint');
// Check for successful API response
if ($response->successful()) {
// Process the successful response
$data = $response->json();
// Process the data further as needed
} else {
throw new Exception('API request failed');
}
} catch (Exception $ex) {
// Handle the exception or log the error
// For simplicity, let's just log the error message here
Log::error($ex->getMessage());
// Optionally, you can return a response with a specific error message
// For example: return response(['error' => 'API request failed'], 500);
}
xxxxxxxxxx
try {
// Perform some action that might throw an exception
$result = $this->performAction();
// Return a success response
return response()->json(['data' => $result], 200);
} catch (\Exception $e) {
// Log the exception
Log::error($e->getMessage());
// Return an error response
return response()->json(['error' => 'An error occurred'], 500);
}