Update Spatie versi 4

This commit is contained in:
2026-11-05 17:06:11 +07:00
parent b76fa9e59b
commit 9cdd178297
11 changed files with 167 additions and 69 deletions
@@ -4,10 +4,12 @@ namespace App\Http\Controllers\Web\Konfigurasi;
use App\Http\Controllers\Controller;
use App\Http\Requests\Konfigurasi\FilterLogAktivitasRequest;
use App\Models\Konfigurasi\LogAktivitas;
use App\Models\Pertanian\Petani;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Support\Facades\DB;
class LogAktivitasController extends Controller
{
@@ -38,21 +40,24 @@ class LogAktivitasController extends Controller
{
$filter = $request->validated();
// Ambil log sesuai filter
$aktivitas = Activity::query()
->with(['causer', 'subject'])
// Ambil log sesuai filter (tanpa COUNT total)
$aktivitas = LogAktivitas::query()
->with('causer:id,name,role')
->when($filter['jenis'] ?? null, fn ($q, $jenis) => $q->where('log_name', $jenis))
->when($filter['dari'] ?? null, fn ($q, $tgl) => $q->where('created_at', '>=', $tgl))
->when($filter['sampai'] ?? null, fn ($q, $tgl) => $q->where('created_at', '<', Carbon::parse($tgl)->addDay()))
->when($filter['search'] ?? null, fn ($q, $cari) => $this->cari($q, '%' . mb_strtolower($cari) . '%'))
->latest('id')
->paginate((int) ($filter['per_page'] ?? self::PER_PAGE[0]))
->simplePaginate((int) ($filter['per_page'] ?? self::PER_PAGE[0]))
->withQueryString();
// Nama petani untuk log tab petani (satu query)
$namaPetani = Petani::withoutGlobalScopes()->withTrashed()
->whereIn('id', $aktivitas->getCollection()->pluck('properties.petani_id')->filter()->unique())
->pluck('nama', 'id');
// Nama petani untuk log yang belum menyimpan nama (satu query)
$idPetani = $aktivitas->getCollection()
->map(fn ($a) => $a->properties['petani_id'] ?? ($a->subject_type === Petani::class ? $a->subject_id : null))
->filter()
->unique();
$namaPetani = $idPetani->isEmpty() ? collect() : Petani::withoutGlobalScopes()->whereIn('id', $idPetani)->pluck('nama', 'id');
return view('konfigurasi.log-aktivitas.index', [
'aktivitas' => $aktivitas,
@@ -62,15 +67,16 @@ class LogAktivitasController extends Controller
]);
}
// Cari di aktivitas, nama pengguna, dan nama petani
private function cari($query, string $cari): void
// Cari di aktivitas, nama pengguna, dan nama petani (memakai index)
private function cari(Builder $query, string $cari): void
{
$petani = Petani::withoutGlobalScopes()->select('id')->whereRaw('lower(nama) like ?', [$cari]);
$pengguna = User::withoutGlobalScopes()->select('id')->whereRaw('lower(name) like ?', [$cari]);
$query->where(fn ($q) => $q
->whereRaw('lower(description) like ?', [$cari])
->orWhereHasMorph('causer', [User::class], fn ($q) => $q->whereRaw('lower(name) like ?', [$cari]))
->orWhereRaw(
"coalesce((properties->>'petani_id')::bigint, case when subject_type = ? then subject_id end) in (select id from petani where lower(nama) like ?)",
[Petani::class, $cari]
));
->orWhere(fn ($q) => $q->where('causer_type', User::class)->whereIn('causer_id', $pengguna))
->orWhere(fn ($q) => $q->where('subject_type', Petani::class)->whereIn('subject_id', $petani))
->orWhereIn(DB::raw("(properties->>'petani_id')::bigint"), $petani));
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Http\Middleware;
use App\Services\Konfigurasi\BufferLogAktivitas;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class SimpanLogAktivitas
{
public function __construct(private BufferLogAktivitas $buffer) {}
public function handle(Request $request, Closure $next): Response
{
$this->buffer->mulai();
return $next($request);
}
// Simpan log setelah respons terkirim; request gagal (5xx) tidak dicatat
public function terminate(Request $request, Response $response): void
{
$response->getStatusCode() >= 500 ? $this->buffer->buang() : $this->buffer->simpan();
}
}