203 lines
6.2 KiB
PHP
203 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admins\Users;
|
|
|
|
use App\Helpers\ResponseFormatter;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\User;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
// fetch data role
|
|
$roles = Role::latest()->get();
|
|
|
|
return view('pages.admin.users.index', compact('roles'));
|
|
}
|
|
|
|
public function getDataUser()
|
|
{
|
|
$users = User::with('role')->latest()->get();
|
|
|
|
return datatables()->of($users)
|
|
->addColumn('rolenya', function ($users) {
|
|
$roles = '';
|
|
foreach ($users->role as $role) {
|
|
$roles .= '<span class="badge badge-primary">' . $role->name . '</span> ';
|
|
}
|
|
return $roles;
|
|
})
|
|
->addColumn('action', function ($users) {
|
|
return '
|
|
<button class="btn btn-sm btn-warning btn-edit-user" data-user=' . $users . '><i class="fas fa-edit"></i></button>
|
|
<button class="btn btn-sm btn-danger btn-hapus-user" data-user=' . $users . '><i class="fas fa-trash"></i></button>
|
|
';
|
|
})
|
|
->addColumn('updated_atnya', function ($users) {
|
|
return tanggal_indonesia($users->updated_at) . ' ' . $users->updated_at->format('H:i:s');
|
|
})
|
|
->rawColumns(['action' => 'action', 'rolenya' => 'rolenya', 'updated_atnya' => 'updated_atnya'])
|
|
->addIndexColumn()
|
|
->make(true);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
// validation
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => 'required',
|
|
'email' => 'required|email|unique:users',
|
|
'password' => 'required|min:8|confirmed',
|
|
'role_id' => 'required'
|
|
], [
|
|
'name.required' => 'Nama tidak boleh kosong!',
|
|
'email.required' => 'Email tidak boleh kosong!',
|
|
'email.email' => 'Email tidak valid!',
|
|
'email.unique' => 'Email sudah terdaftar!',
|
|
'password.required' => 'Password tidak boleh kosong!',
|
|
'password.min' => 'Password minimal 8 karakter!',
|
|
'password.confirmed' => 'Password tidak sama!',
|
|
'role_id.required' => 'Role tidak boleh kosong!',
|
|
]);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
return ResponseFormatter::error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// create new account
|
|
$user = User::create([
|
|
'name' => $request->name,
|
|
'email' => $request->email,
|
|
'password' => bcrypt($request->password),
|
|
]);
|
|
|
|
// Create role for user
|
|
foreach ($request->role_id as $key => $value) {
|
|
$role = Role::findOrFail($value); // Pengunjung
|
|
$user->assignRole($role);
|
|
}
|
|
DB::commit();
|
|
return ResponseFormatter::success($user, "User berhasil ditambahkan");
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
return ResponseFormatter::error($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
// get data from user
|
|
$user = User::with('role')->findOrFail($id);
|
|
return ResponseFormatter::success([
|
|
'user' => $user
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
// validation
|
|
$validator = Validator::make($request->all(), [
|
|
'name_edit' => 'required',
|
|
'email_edit' => 'required|email|unique:users,email,' . $id,
|
|
'role_id_edit' => 'required'
|
|
], [
|
|
'name_edit.required' => 'Nama tidak boleh kosong!',
|
|
'email_edit.required' => 'Email tidak boleh kosong!',
|
|
'email_edit.email' => 'Email tidak valid!',
|
|
'email_edit.unique' => 'Email sudah terdaftar!',
|
|
'role_id.required' => 'Role tidak boleh kosong!',
|
|
]);
|
|
|
|
// check validation
|
|
if ($validator->fails()) {
|
|
return ResponseFormatter::error($validator->errors()->first());
|
|
}
|
|
|
|
try {
|
|
DB::beginTransaction();
|
|
|
|
// create new account
|
|
$user = User::findOrFail($id);
|
|
$user->update([
|
|
'name' => $request->name_edit,
|
|
'email' => $request->email_edit,
|
|
]);
|
|
|
|
// Password change
|
|
if ($request->password_edit) {
|
|
$user->update([
|
|
'password' => bcrypt($request->password_edit),
|
|
]);
|
|
}
|
|
|
|
// Delete role for user
|
|
$user->roles()->detach();
|
|
|
|
// Assing role for user
|
|
foreach ($request->role_id_edit as $key => $value) {
|
|
$role = Role::findOrFail($value); // Pengunjung
|
|
$user->assignRole($role);
|
|
}
|
|
|
|
DB::commit();
|
|
return ResponseFormatter::success($user, "User berhasil diubah");
|
|
} catch (\Throwable $th) {
|
|
DB::rollBack();
|
|
return ResponseFormatter::error($th->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
// get data from user
|
|
$user = User::findOrFail($id);
|
|
|
|
// check user
|
|
if (!$user) {
|
|
return ResponseFormatter::error("Data pengguna tidak ditemukan!");
|
|
}
|
|
|
|
// delete user
|
|
$user->delete();
|
|
|
|
return ResponseFormatter::success(null, "User berhasil dihapus!");
|
|
}
|
|
}
|