-
app/Http/Controllers/UserDashboardController.php
Open in GitHubuse App\Models\User; use Illuminate\Validation\Rule; class UserDashboardController extends Controller { // public function index() { $users = User::orderByDesc('id') ->select('id', 'name', 'email', 'created_at', 'roles', 'stripe_id') ->simplePaginate(10); $stripeCustomer = $users[0]->createOrGetStripeCustomer(); return view('usersDashboard.index', [ 'users' => $users, ]); } public function edit($id) { $user = User::find($id); return view('usersDashboard.edit', [ 'user' => $user, ]); } public function update(Request $request, $id) { $request->validate([ 'name' => 'required|string|max:255', 'email' => [ 'required', 'max:255', 'string', 'email', Rule::unique('users', 'email')->ignore($id) ], 'roles' => 'required|string|max:25', ]); $user = User::find($id); $user->name = $request->name; $user->email = $request->email; $user->roles = $request->roles; $user->save(); return redirect()->route('usersDashboard'); } }
-
resources/views/usersDashboard/index.blade.php
Open in GitHub@extends('layouts.dashboard') @section('title') All users - @endsection @section('pageTitle') All users @endsection @section('content') <div class="table-responsive"> <table class="table"> <thead class="thead-light"> <tr> <th scope="col">#</th> <th scope="col">Name</th> <th scope="col">Email</th> <th scope="col">Created at</th> <th scope="col">Role</th> <th scope="col">Action</th> </tr> </thead> <tbody> @foreach ($users as $user) <tr> <th scope="row">{{$loop->index + 1}}</th> <td>{{$user->name}}</td> <td>{{$user->email}}</td> <td>{{$user->created_at}}</td> <td> @if($user->roles == 'admin') Administrator @elseif($user->roles == 'user') User @endif </td> <td> <a class="btn btn-primary my-2" href="{{route('usersEdit', ['id' => $user->id])}}" style="color:white;text-decoration:none;"> Edit </a> <a class="btn btn-primary my-2" href="{{route('usersSubscriptionDetails', ['id' => $user->id])}}" style="color:white;text-decoration:none;"> Subscription details </a> </td> </tr> @endforeach </tbody> </table> </div> {{ $users->links() }} @endsection
-
resources/views/usersDashboard/edit.blade.php
Open in GitHub@extends('layouts.dashboard') @section('title') Edit {{$user['name']}} - @endsection @section('pageTitle') Edit {{$user['name']}} @endsection @section('content') <div class="container"> <div class="row"> <div class="col"> <form action="{{ route('usersUpdate', ['id' => $user['id']]) }}" method="POST"> @csrf <div class="form-group"> <label for="name">Name</label> <input type="text" name="name" class="form-control" id="name" value="{{ old('name') ? old('name') : $user['name'] }}"> @error('name') <div class="alert alert-danger py-2 my-2">{{ $message }}</div> @enderror </div> <div class="form-group"> <label for="email">Email</label> <input type="text" name="email" class="form-control" id="email" value="{{ old('email') ? old('email') : $user['email'] }}"> @error('email') <div class="alert alert-danger py-2 my-2">{{ $message }}</div> @enderror </div> <div class="form-group"> <label for="roles">Role</label><br> <select name="roles" class="custom-select" id="roles" value="{{ old('roles') ? old('roles') : $user['roles'] }}"> <option value="user" @if (old('roles') == "user" || $user['roles'] == "user") selected @endif>User</option> <option value="admin" @if (old('roles') == "admin" || $user['roles'] == "admin") selected @endif>Administrator</option> </select> </div> <input type="submit" class="btn btn-primary my-2"> </form> </div> </div> </div> @endsection