Update Module User

- Add Nik and branch,
This commit is contained in:
Daeng Deni Mardaeni 2024-08-27 14:45:50 +07:00
parent 18f0f68f1a
commit d3baaa31c1
8 changed files with 121 additions and 15 deletions

View File

@ -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
];
}
}

View File

@ -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()

View File

@ -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),
]);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Modules\Lpj\Models\Branch;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->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');
});
}
};

View File

@ -20,7 +20,7 @@
"classes": "",
"attributes": [],
"permission": "",
"roles": [],
"roles": ["Administrator"],
"sub": [
{
"title": "Users",

View File

@ -52,6 +52,43 @@
@enderror
</div>
</div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
NIK
</label>
<div class="flex flex-wrap items-baseline w-full">
<input class="w-full input @error('nik') border-danger @enderror" type="number" name="nik" value="{{ $user->nik ?? '' }}">
@error('nik')
<em class="alert text-danger text-sm">{{ $message }}</em>
@enderror
</div>
</div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Branch
</label>
<div class="flex flex-wrap items-baseline w-full">
<select class="input tomselect w-full @error('branch_id') border-danger @enderror" name="branch_id" id="branch_id">
<option value="">Pilih Branch</option>
@if(isset($branches))
@foreach($branches as $row)
@if(isset($user))
<option value="{{ $row->id }}" {{ isset($user->branch_id) && $user->branch_id == $row->id?'selected' : '' }}>
{{ $row->name }}
</option>
@else
<option value="{{ $row->id }}">
{{ $row->name }}
</option>
@endif
@endforeach
@endif
</select>
@error('branch_id')
<em class="alert text-danger text-sm">{{ $message }}</em>
@enderror
</div>
</div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Password

View File

@ -64,6 +64,14 @@
<span class="sort"> <span class="sort-label"> Email </span>
<span class="sort-icon"> </span> </span>
</th>
<th class="min-w-[185px]" data-datatable-column="nik">
<span class="sort"> <span class="sort-label"> NIK </span>
<span class="sort-icon"> </span> </span>
</th>
<th class="min-w-[185px]" data-datatable-column="branch">
<span class="sort"> <span class="sort-label"> Branch </span>
<span class="sort-icon"> </span> </span>
</th>
<th class="min-w-[50px] text-center" data-datatable-column="actions">Action</th>
</tr>
</thead>
@ -145,6 +153,15 @@
email: {
title: 'Email',
},
nik: {
title: 'NIK',
},
branch: {
title: 'Branch',
render: (item, data) => {
return data.branch.name;
},
},
actions: {
title: 'Status',
render: (item, data) => {