@php use Illuminate\Support\Facades\Log; header("Access-Control-Allow-Origin: *"); // Permite todas as origens header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { http_response_code(200); exit(); } $body = file_get_contents('php://input'); $func = json_decode($body, true); // Caminho para o arquivo JSON $jsonFilePath = public_path('json/funcionarios.json'); // Tenta carregar o arquivo JSON existente if (file_exists($jsonFilePath)) { $data = json_decode(file_get_contents($jsonFilePath), true); } else { $data = []; } // Verifique se $func é null e inicialize como um array vazio se for if (is_null($func)) { $func = []; } foreach ($func as $key => $value) { $data[$key] = $value; } try { file_put_contents($jsonFilePath, json_encode($data, JSON_PRETTY_PRINT)); Log::info("Arquivo atualizado com sucesso."); } catch (Exception $e) { echo "Erro ao salvar o arquivo: " . $e->getMessage(); Log::error("Ocorreu um erro crítico." . $e->getMessage()); } @endphp