SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Admin;
  4.  
  5. use App\User;
  6. use Facades\App\Helpers\Json;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\Controller;
  9.  
  10. class UserController extends Controller
  11. {
  12.     /**
  13.      * Display a listing of the resource.
  14.      *
  15.      * @return \Illuminate\Http\Response
  16.      */
  17.     public function index(Request $request)
  18.     {
  19.         $orderby = [
  20.             ["DisplayName" => "Name (A => Z)","sortfield" => "name", "sortorder" => "asc" ],
  21.             ["DisplayName" => "Name (Z => A)","sortfield" => "name", "sortorder" => "desc" ],
  22.             ["DisplayName" => "Email (A => Z)","sortfield" => "email", "sortorder" => "asc" ],
  23.             ["DisplayName" => "Email (Z => A)","sortfield" => "name", "sortorder" => "desc" ],
  24.             ["DisplayName" => "Not Active","sortfield" => "active", "sortorder" => "asc" ],
  25.             ["DisplayName" => "Admin","sortfield" => "admin", "sortorder" => "desc" ]
  26.         ];
  27.         $artist_title = '%' . $request->input('artist') . '%';
  28.         $users = User::orderBy($orderby[$request->sort] ["sortfield"] ?? "name",$orderby[$request->sort] ["sortorder"] ?? "asc")
  29.             ->where(function ($query) use ($artist_title) {
  30.                 $query->where('name', 'like', $artist_title);
  31.             })
  32.             ->orwhere(function ($query) use ($artist_title) {
  33.                 $query->where('email', 'like', $artist_title);
  34.             })
  35.             ->paginate(12);
  36.         $result = compact('users','orderby');
  37.         Json::dump($result);
  38.         return view('admin.users.index', $result);
  39.  
  40.     }
  41.  
  42.     /**
  43.      * Show the form for creating a new resource.
  44.      *
  45.      * @return \Illuminate\Http\Response
  46.      */
  47.     public function create()
  48.     {
  49.         //
  50.     }
  51.  
  52.     /**
  53.      * Store a newly created resource in storage.
  54.      *
  55.      * @param  \Illuminate\Http\Request  $request
  56.      * @return \Illuminate\Http\Response
  57.      */
  58.     public function store(Request $request)
  59.     {
  60.         //
  61.     }
  62.  
  63.     /**
  64.      * Display the specified resource.
  65.      *
  66.      * @param  \App\User  $user
  67.      * @return \Illuminate\Http\Response
  68.      */
  69.     public function show(User $user)
  70.     {
  71.         //
  72.     }
  73.  
  74.     /**
  75.      * Show the form for editing the specified resource.
  76.      *
  77.      * @param  \App\User  $user
  78.      * @return \Illuminate\Http\Response
  79.      */
  80.     public function edit(User $user)
  81.     {
  82.         $currentuser = auth()->user();
  83.         if($user->id == $currentuser->id ){
  84.             session()->flash('danger', "The user <b>$user->name</b> can not been editted. You can't edit the currently logged in user");
  85.             return redirect('admin/users');
  86.         }else{
  87.             $result = compact('user');
  88.             Json::dump($result);
  89.             return view('admin.users.edit', $result);
  90.         }
  91.  
  92.     }
  93.  
  94.     /**
  95.      * Update the specified resource in storage.
  96.      *
  97.      * @param  \Illuminate\Http\Request  $request
  98.      * @param  \App\User  $user
  99.      * @return \Illuminate\Http\Response
  100.      */
  101.     public function update(Request $request, User $user)
  102.     {
  103.         $currentuser = auth()->user();
  104.         $this->validate($request,[
  105.             'name' => 'required|min:3|',
  106.             'email' => 'required|email|unique:users,email,' . $user->id
  107.  
  108.         ]);
  109.  
  110.         if($user->id == $currentuser->id ){
  111.             session()->flash('danger', "The user <b>$user->name</b> can not been editted. You can't edit the currently logged in user");
  112.             return redirect('admin/users');
  113.         }else{
  114.             $user->name = $request->name;
  115.             $user->email = $request->email;
  116.             $user->active = $request->active ?? '0';
  117.             $user->admin = $request->admin ?? '0';
  118.             $user->save();
  119.             session()->flash('success', 'user: ' . "<b>$user->name</b>" . ' has been updated');
  120.             return redirect('admin/users');
  121.         }
  122.  
  123.     }
  124.  
  125.     /**
  126.      * Remove the specified resource from storage.
  127.      *
  128.      * @param  \App\User  $user
  129.      * @return \Illuminate\Http\Response
  130.      */
  131.     public function destroy(User $user)
  132.     {
  133.         $currentuser = auth()->user();
  134.         if($user->id == $currentuser->id ){
  135.             session()->flash('danger', "The user <b>$user->name</b> can not been deleted. You can't delete the currently logged in user");
  136.             return redirect('admin/users');
  137.         }else{
  138.             $user->delete();
  139.             session()->flash('success', "The user <b>$user->name</b> has been deleted");
  140.             return redirect('admin/users');
  141.         }
  142.  
  143.  
  144.     }
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top