first&last
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\System;
|
||||
|
||||
use App\Exports\CsvExport;
|
||||
use App\Models\ActivityLog;
|
||||
use App\Models\User;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
#[Layout('layouts.app')]
|
||||
class ActivityLogPage extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
public string $userId = '';
|
||||
|
||||
public string $action = '';
|
||||
|
||||
public string $dateFrom = '';
|
||||
|
||||
public string $dateTo = '';
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
abort_unless(auth()->user()->isAdmin(), 403);
|
||||
}
|
||||
|
||||
public function exportCsv()
|
||||
{
|
||||
$rows = $this->query()->get()->map(fn (ActivityLog $log) => [
|
||||
$log->user?->name,
|
||||
$log->action,
|
||||
class_basename($log->model_type),
|
||||
$log->description,
|
||||
$log->ip_address,
|
||||
$log->created_at?->format('d.m.Y H:i:s'),
|
||||
])->toArray();
|
||||
|
||||
return CsvExport::download('activity-log.csv', ['Пользователь', 'Действие', 'Сущность', 'Описание', 'IP', 'Дата'], $rows);
|
||||
}
|
||||
|
||||
private function query()
|
||||
{
|
||||
return ActivityLog::query()
|
||||
->with('user')
|
||||
->when($this->userId !== '', fn ($q) => $q->where('user_id', $this->userId))
|
||||
->when($this->action !== '', fn ($q) => $q->where('action', 'like', "%{$this->action}%"))
|
||||
->when($this->dateFrom !== '', fn ($q) => $q->whereDate('created_at', '>=', $this->dateFrom))
|
||||
->when($this->dateTo !== '', fn ($q) => $q->whereDate('created_at', '<=', $this->dateTo))
|
||||
->latest('created_at');
|
||||
}
|
||||
|
||||
public function render()
|
||||
{
|
||||
return view('livewire.system.activity-log-page', [
|
||||
'logs' => $this->query()->paginate(20),
|
||||
'users' => User::query()->orderBy('name')->get(),
|
||||
])->title('Журнал действий');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user