• File: BusinessLogController.php
  • Full Path: /var/www/imaliapi/app/Http/Controllers/BusinessLogController.php
  • Date Modified: 10/29/2025 5:32 PM
  • File size: 1.12 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php

namespace App\Http\Controllers;

use App\UserActivityLog;
use Illuminate\Http\Request;

class BusinessLogController extends Controller
{
    //
    public function activity_log()
    {
        $logs = UserActivityLog::with('user:id,name,email')
            ->orderBy('created_at', 'desc')
            ->paginate(50);

        // Mapear e formatar o campo request_data
        $formattedLogs = $logs->getCollection()->map(function ($log) {
            $decoded = json_decode($log->request_data, true);

            // Caso o JSON seja inválido ou vazio, retorna array vazio
            $log->request_data = $decoded ?: [];
            return $log;
        });

        // Substituir a coleção paginada pelo conteúdo formatado
        $logs->setCollection($formattedLogs);

        return response()->json([
            'success' => true,
            'total' => $logs->total(),
            'data' => $logs->items(),
            'pagination' => [
                'current_page' => $logs->currentPage(),
                'last_page' => $logs->lastPage(),
                'per_page' => $logs->perPage(),
            ],
        ]);
    }
}