diff --git a/app/Exports/UsersExport.php b/app/Exports/UsersExport.php index 747e342..4ee9169 100644 --- a/app/Exports/UsersExport.php +++ b/app/Exports/UsersExport.php @@ -20,6 +20,8 @@ class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection, $row->id, $row->name, $row->email, + $row->nik, + $row->branch->name, $row->created_at ]; } @@ -28,6 +30,8 @@ class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection, 'ID', 'Name', 'Email', + 'NIK', + 'Branch', 'Created At' ]; } @@ -35,7 +39,7 @@ class UsersExport implements WithColumnFormatting, WithHeadings, FromCollection, public function columnFormats(): array{ return [ 'A' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER, - 'C' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME + 'F' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DATETIME ]; } } diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index baca7ab..0ce25da 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -5,6 +5,7 @@ use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Maatwebsite\Excel\Facades\Excel; + use Modules\Lpj\Models\Branch; use Modules\Usermanagement\Exports\UsersExport; use Modules\Usermanagement\Http\Requests\User as UserRequest; use Modules\Usermanagement\Models\Role; @@ -73,8 +74,9 @@ if ($request->has('search') && !empty($request->get('search'))) { $search = $request->get('search'); $query->where(function ($q) use ($search) { - $q->where('name', 'LIKE', "%$search%") - ->orWhere('email', 'LIKE', "%$search%"); + $q + ->where('name', 'LIKE', "%$search%") + ->orWhere('email', 'LIKE', "%$search%"); }); } @@ -101,10 +103,10 @@ $filteredRecords = $query->count(); // Get the data for the current page - $users = $query->get(); + $users = $query->with('branch')->get(); // Calculate the page count - $pageCount = ceil($totalRecords/$request->get('size')); + $pageCount = ceil($totalRecords / $request->get('size')); // Calculate the current page number $currentPage = 0 + 1; @@ -135,9 +137,10 @@ //abort(403, 'Sorry! You are not allowed to edit users.'); } - $user = User::find($id); - $roles = Role::all(); - return view('usermanagement::users.create', compact('user', 'roles')); + $user = User::find($id); + $roles = Role::all(); + $branches = Branch::all(); + return view('usermanagement::users.create', compact('user', 'roles', 'branches')); } /** @@ -239,8 +242,9 @@ //abort(403, 'Sorry! You are not allowed to create a user.'); } - $roles = Role::all(); - return view('usermanagement::users.create', compact('roles')); + $roles = Role::all(); + $branches = Branch::all(); + return view('usermanagement::users.create', compact('roles', 'branches')); } public function export() diff --git a/app/Http/Requests/User.php b/app/Http/Requests/User.php index 98b0b0d..632e2e3 100644 --- a/app/Http/Requests/User.php +++ b/app/Http/Requests/User.php @@ -20,13 +20,15 @@ public function rules() : array { - $rules = [ - 'name' => 'required|string|max:255', + 'name' => 'required|string|max:255', + 'nik' => 'nullable|string|max:6|unique:users,nik', + 'branch_id' => 'nullable|exists:branches,id', + 'profile_photo_path' => 'nullable|image|mimes:jpeg,png,jpg|max:2048', ]; if ($this->password || $this->method() === 'POST') { - $rules['email'] = 'required|email|unique:users,email'; + $rules['email'] = 'required|email|unique:users,email'; $rules['password'] = 'required|string|min:8|confirmed'; } @@ -40,7 +42,7 @@ public function passedValidation() { $this->merge([ - 'password' => Hash::make($this->password) + 'password' => Hash::make($this->password), ]); } } diff --git a/app/Models/User.php b/app/Models/User.php index 03de691..e458631 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; + use Modules\Lpj\Models\Branch; use Spatie\Permission\Traits\HasRoles; use Wildside\Userstamps\Userstamps; @@ -38,6 +39,11 @@ 'name', 'email', 'password', + 'nik', + 'branch_id', + 'profile_photo_path', + 'last_login_at', + 'last_login_ip', ]; /** @@ -69,5 +75,9 @@ 'id' => 'string', ]; } + + public function branch(){ + return $this->belongsTo(Branch::class); + } } diff --git a/database/migrations/2024_08_27_071158_update_users_table.php b/database/migrations/2024_08_27_071158_update_users_table.php new file mode 100644 index 0000000..8ae0073 --- /dev/null +++ b/database/migrations/2024_08_27_071158_update_users_table.php @@ -0,0 +1,32 @@ +string('nik')->nullable()->after('email'); + $table->foreignIdFor(Branch::class)->nullable()->after('nik')->constrained('branches'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + $table->dropColumn('nik'); + $table->dropForeign(['branch_id']); + $table->dropColumn('branch_id'); + }); + } +}; diff --git a/module.json b/module.json index 18768a6..fc1ba48 100644 --- a/module.json +++ b/module.json @@ -20,7 +20,7 @@ "classes": "", "attributes": [], "permission": "", - "roles": [], + "roles": ["Administrator"], "sub": [ { "title": "Users", diff --git a/resources/views/users/create.blade.php b/resources/views/users/create.blade.php index a54fd2b..2e9f743 100644 --- a/resources/views/users/create.blade.php +++ b/resources/views/users/create.blade.php @@ -52,6 +52,43 @@ @enderror +
+ +
+ + @error('nik') + {{ $message }} + @enderror +
+
+
+ +
+ + @error('branch_id') + {{ $message }} + @enderror +
+