validated(); $data['user_id'] = Auth::id(); // Check if the user already reported the deal $alreadyReported = $deal->reports()->where('user_id', Auth::id())->exists(); if ($alreadyReported) { return response()->json(['message' => 'You had already reported this deal'], 200); } try { DB::transaction(function () use ($data, $deal) { Report::unguard(); Report::create($data)->deals()->attach($deal); Report::reguard(); }); return response()->json(['message' => 'Report submitted. Thank you for keeping DealHub safe'], 201); } catch (\Throwable $exception) { Log::error('Error creating report', [ 'user_id' => Auth::id(), 'deal_id' => $deal->id, 'error' => $exception->getMessage(), 'trace' => $exception->getTraceAsString(), ]); return response()->json(['message' => 'Error creating report'], 500); } } /** * Display the specified resource. */ public function show(Report $report) { // } /** * Update the specified resource in storage. */ public function update(Request $request, Report $report) { // } /** * Remove the specified resource from storage. */ public function destroy(Report $report) { try { DB::transaction(function () use ($report) { $report->deals()->detach(); $report->delete(); }); return back()->with('success', 'Report deleted successfully.'); } catch (\Throwable $e) { Log::error('Error deleting report', [ 'user_id' => Auth::id(), 'report_id' => $report->id, 'error' => $e->getMessage(), ]); } return back()->with('error', 'Something went wrong.'); } }