Update Module User, Add Role Selection

This commit is contained in:
Daeng Deni Mardaeni 2024-08-28 08:58:35 +07:00
parent d3baaa31c1
commit 18b8ed5344
2 changed files with 170 additions and 119 deletions

View File

@ -161,6 +161,11 @@
$user = User::find($id); $user = User::find($id);
$user->update($request->all()); $user->update($request->all());
if ($request->roles) {
$user->roles()->detach();
$user->assignRole($request->roles);
}
return redirect()->route('users.index')->with('success', 'User updated successfully.'); return redirect()->route('users.index')->with('success', 'User updated successfully.');
} }
@ -223,6 +228,10 @@
$user = User::create($validated); $user = User::create($validated);
if ($user) { if ($user) {
if ($request->roles) {
$user->assignRole($request->roles);
}
return redirect()->route('users.index')->with('success', 'User created successfully.'); return redirect()->route('users.index')->with('success', 'User created successfully.');
} }
} }

View File

@ -124,6 +124,48 @@
@enderror @enderror
</div> </div>
</div> </div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Role
</label>
<div class="flex flex-wrap items-baseline w-full">
<div class="grid grid-cols-1 lg:grid-cols-2 gap-5 py-5 lg:py-7.5 w-full">
@foreach($roles as $role)
<div class="rounded-xl border p-4 flex items-center justify-between gap-2.5">
<div class="flex items-center gap-3.5">
<div class="relative size-[45px] shrink-0">
<svg class="w-full h-full stroke-gray-300 fill-gray-100" fill="none" height="48" viewBox="0 0 44 48" width="44" xmlns="http://www.w3.org/2000/svg">
<path d="M16 2.4641C19.7128 0.320509 24.2872 0.320508 28 2.4641L37.6506 8.0359C41.3634 10.1795 43.6506 14.141 43.6506
18.4282V29.5718C43.6506 33.859 41.3634 37.8205 37.6506 39.9641L28 45.5359C24.2872 47.6795 19.7128 47.6795 16 45.5359L6.34937
39.9641C2.63655 37.8205 0.349365 33.859 0.349365 29.5718V18.4282C0.349365 14.141 2.63655 10.1795 6.34937 8.0359L16 2.4641Z" fill="">
</path>
<path d="M16.25 2.89711C19.8081 0.842838 24.1919 0.842837 27.75 2.89711L37.4006 8.46891C40.9587 10.5232 43.1506 14.3196 43.1506
18.4282V29.5718C43.1506 33.6804 40.9587 37.4768 37.4006 39.5311L27.75 45.1029C24.1919 47.1572 19.8081 47.1572 16.25 45.1029L6.59937
39.5311C3.04125 37.4768 0.849365 33.6803 0.849365 29.5718V18.4282C0.849365 14.3196 3.04125 10.5232 6.59937 8.46891L16.25 2.89711Z" stroke="">
</path>
</svg>
<div class="absolute leading-none left-2/4 top-2/4 -translate-y-2/4 -translate-x-2/4">
<i class="ki-filled ki-category text-lg text-gray-500">
</i>
</div>
</div>
<div class="flex flex-col gap-1">
<span class="flex items-center gap-1.5 leading-none font-medium text-sm text-gray-900">
{{ $role->name }}
</span>
<span class="text-2sm text-gray-700">
</span>
</div>
</div>
<div class="switch switch-sm">
<input {{ in_array($role->name,Auth()->user()->roles->pluck('name')->toArray()) ? 'checked' : '' }} name="roles" type="radio" value="{{ $role->id }}">
</div>
</div>
@endforeach
</div>
</div>
</div>
<div class="flex justify-end"> <div class="flex justify-end">
<button type="submit" class="btn btn-primary"> <button type="submit" class="btn btn-primary">