110 lines
2.4 KiB
PHP
110 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Usercategory;
|
|
use Request;
|
|
use App\User;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$users=User::all();
|
|
return view('admin.user.index',compact('users'));
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$usercat = Usercategory::all();
|
|
return view('admin.user.create',compact( 'usercat'));
|
|
}
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function store()
|
|
{
|
|
$user=Request::all();
|
|
$user['password'] = bcrypt($user['password']);
|
|
|
|
$newUser = User::create($user);
|
|
if(isset($user["usercategory"])){
|
|
$newUser->usercategory()->sync($user["usercategory"]);
|
|
}
|
|
$newUser->save();
|
|
|
|
return redirect('admin/user');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$user=User::find($id);
|
|
$user->password = "";
|
|
$usercat = Usercategory::all();
|
|
|
|
return view('admin.user.edit',compact('user', 'usercat'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function update($id)
|
|
{
|
|
$userUpdate = Request::all();
|
|
|
|
|
|
$user=User::find($id);
|
|
$user->title = $userUpdate["title"];
|
|
$user->firstname = $userUpdate["firstname"];
|
|
$user->lastname = $userUpdate["lastname"];
|
|
$user->street = $userUpdate["street"];
|
|
$user->location = $userUpdate["location"];
|
|
$user->phone = $userUpdate["phone"];
|
|
$user->email = $userUpdate["email"];
|
|
$user->notes = $userUpdate["notes"];
|
|
|
|
if(isset($userUpdate["usercategory"]))
|
|
$user->usercategory()->sync($userUpdate["usercategory"]);
|
|
|
|
|
|
if($userUpdate["password"] !== null)
|
|
$user->password = bcrypt($userUpdate['password']);
|
|
$user->save();
|
|
|
|
|
|
return redirect('admin/user');
|
|
}
|
|
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$user = User::find($id);
|
|
$user->usercategory()->detach();
|
|
$user->delete();
|
|
return redirect('admin/user');
|
|
}
|
|
}
|