diff --git a/app/Exports/UsersExport.php b/app/Exports/UsersExport.php index 4ee9169..ce9831c 100644 --- a/app/Exports/UsersExport.php +++ b/app/Exports/UsersExport.php @@ -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 ]; } } diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 33f3965..6aa8cc9 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -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', ]); diff --git a/resources/views/users/index.blade.php b/resources/views/users/index.blade.php index f28a717..0642830 100644 --- a/resources/views/users/index.blade.php +++ b/resources/views/users/index.blade.php @@ -7,8 +7,9 @@ @section('content')
| - - | -- Name - - | -- Email - - | -- NIK - - | -- Branch - - | -- Role - - | -Action | -
|---|---|---|---|---|---|---|
| + + | ++ Name + + | ++ Email + + | ++ NIK + + | ++ Branch + + | ++ Role + + | +Action | +