(usermanagement): tambah fitur export users dengan filter pencarian

- Modifikasi `UsersExport` untuk menerima parameter search
- Tambah filter whereAny (name, email) di `collection()`
- Tambah kolom "Roles" dengan mapping roles (pluck + implode)
- Null safety branch pakai optional chaining
- Update formatting kolom export
- Modifikasi `UsersController@export` untuk terima & teruskan search
- Batasi role berdasarkan role user login
- Konsistensikan pencarian di `index()` pakai whereAny
- Hapus validasi NIK di profile update
- Tambah ID pada tombol export di `index.blade.php`
- Tambah fungsi JS `updateExportUrl()` untuk sinkronisasi search
- Null safety render branch & role di DataTable
- Tambah listener untuk update URL export saat search
- Perbaiki formatting & indentasi kode
This commit is contained in:
Daeng Deni Mardaeni
2025-08-15 08:35:06 +07:00
parent e3c7bf711c
commit c348af2484
3 changed files with 84 additions and 51 deletions

View File

@@ -9,10 +9,21 @@ use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Modules\Usermanagement\Models\User;
class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection, WithMapping
{
protected $search;
public function __construct($search = null)
{
$this->search = $search;
}
public function collection(){
return User::all();
return User::query()
->when($this->search, function ($query) {
$query->whereAny(['name','email'],'like','%'.$this->search.'%');
})
->get();
}
public function map($row): array{
@@ -21,7 +32,8 @@ class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection,
$row->name,
$row->email,
$row->nik,
$row->branch->name,
$row->branch?->name,
$row->roles?->pluck('name')->implode(', '),
$row->created_at
];
}
@@ -32,6 +44,7 @@ class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection,
'Email',
'NIK',
'Branch',
'Roles',
'Created At'
];
}
@@ -39,7 +52,7 @@ class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection,
public function columnFormats(): array{
return [
'A' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER,
'F' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
'G' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME
];
}
}

View File

@@ -82,10 +82,7 @@
// Apply search filter if provided
if ($request->has('search') && !empty($request->get('search'))) {
$search = $request->get('search');
$query->where(function ($q) use ($search) {
$q->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%'])
->orWhereRaw('LOWER(email) LIKE ?', ['%' . strtolower($search) . '%']);
});
$query->whereAny(['name','email'],'like','%'.$search.'%');
}
// Apply sorting if provided
@@ -241,13 +238,16 @@
return view('usermanagement::users.create', compact('roles', 'branches'));
}
public function export()
public function export(Request $request)
{
if (is_null($this->user) || !$this->user->can('usermanagement.export')) {
abort(403, 'Sorry! You are not allowed to export users.');
}
return Excel::download(new UsersExport, 'users.xlsx');
// Get search parameter from request
$search = $request->get('search');
return Excel::download(new UsersExport($search), 'users.xlsx');
}
public function profile()
@@ -263,7 +263,6 @@
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $user->id,
'nik' => 'required|string|max:255|unique:users,nik,' . $user->id,
'sign' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
]);