123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Models\Staff;
- use App\Models\Role;
- use App\Models\User;
- use Hash;
- class StaffController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $staffs = Staff::paginate(10);
- return view('backend.staff.staffs.index', compact('staffs'));
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- $roles = Role::all();
- return view('backend.staff.staffs.create', compact('roles'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- if(User::where('email', $request->email)->first() == null){
- $user = new User;
- $user->name = $request->name;
- $user->email = $request->email;
- $user->phone = $request->mobile;
- $user->user_type = "staff";
- $user->password = Hash::make($request->password);
- if($user->save()){
- $staff = new Staff;
- $staff->user_id = $user->id;
- $staff->role_id = $request->role_id;
- if($staff->save()){
- flash(translate('Staff has been inserted successfully'))->success();
- return redirect()->route('staffs.index');
- }
- }
- }
- flash(translate('Email already used'))->error();
- return back();
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- //
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $staff = Staff::findOrFail(decrypt($id));
- $roles = Role::all();
- return view('backend.staff.staffs.edit', compact('staff', 'roles'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $staff = Staff::findOrFail($id);
- $user = $staff->user;
- $user->name = $request->name;
- $user->email = $request->email;
- $user->phone = $request->mobile;
- if(strlen($request->password) > 0){
- $user->password = Hash::make($request->password);
- }
- if($user->save()){
- $staff->role_id = $request->role_id;
- if($staff->save()){
- flash(translate('Staff has been updated successfully'))->success();
- return redirect()->route('staffs.index');
- }
- }
- flash(translate('Something went wrong'))->error();
- return back();
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- User::destroy(Staff::findOrFail($id)->user->id);
- if(Staff::destroy($id)){
- flash(translate('Staff has been deleted successfully'))->success();
- return redirect()->route('staffs.index');
- }
- flash(translate('Something went wrong'))->error();
- return back();
- }
- }
|