You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::orderBy('created_at','desc')->paginate(5);
return view('users.index', ['users' => $users]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('users.form');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|confirmed|min:8',
'role' => 'required|in:SuperAdmin,Admin,Bendahara'
]);
if(User::create($request->input())){
return redirect()->route('user.index')->with([
'type' => 'success',
'msg' => 'Pengguna ditambahkan'
]);
}else{
return redirect()->route('user.index')->with([
'type' => 'danger',
'msg' => 'Err.., Terjadi Kesalahan'
]);
}
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
return view('users.form', ['user' => $user]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, User $user)
{
$request->validate([
'name' => 'required|max:255',
'email' => 'required|email|unique:users,email,'.$user->id,
'password' => 'nullable|confirmed|min:8',
'role' => 'required|in:SuperAdmin,Admin,Bendahara'
]);
if($request->password != null){
$user->fill($request->input());
}else{
$user->fill($request->except('password'));
}
if($user->save()){
return redirect()->route('user.index')->with([
'type' => 'success',
'msg' => 'Pengguna diubah'
]);
}else{
return redirect()->route('user.index')->with([
'type' => 'danger',
'msg' => 'Err.., Terjadi Kesalahan'
]);
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(User $user)
{
if($user->delete()){
return redirect()->route('user.index')->with([
'type' => 'success',
'msg' => 'Pengguna dihapus'
]);
}else{
return redirect()->route('user.index')->with([
'type' => 'danger',
'msg' => 'Err.., Terjadi Kesalahan'
]);
}
}
}