WordPress-Like Blog Laravel 5.7 and AdminLTE 3 (16) – Manage Users
In this sixteenth part of creating WordPress-Like Blog using Laravel 5.7 and AdminLTE 3, we will :
- Add Users Menu
- Display Users
- Add User
- Edit User
- Delete User
Add Users Menu
Add users menu inside sidebar.blade.php
1 2 3 4 5 6 7 8 |
<li class="nav-item has-treeview"> <a href="{{ route('backend.user.index') }}" class="nav-link"> <i class="nav-icon fa fa-users"></i> <p> Users </p> </a> </li> |
Display Users
Add another routes :
1 |
Route::resource('/backend/user', 'Backend\UsersController',['as'=>'backend']); |
Create UsersController.php inside App\Http folder, just copy from CategoryController.php that we have and do some proper modifications.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<?php namespace App\Http\Controllers\Backend; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Http\Requests; use App\User; class UsersController extends BackendController { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users = User::orderBy('name')->paginate($this->limit); $usersCount = User::count(); return view ("backend.user.index", compact('users','usersCount')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $user = new User(); return view("backend.user.create", compact('user')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Requests\UserStoreRequest $request) { User::create($request->all()); return redirect("/backend/user")->with('message', 'A New User has been added'); } /** * 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) { $user = User::findOrFail($id); return view("backend.user.edit", compact('user')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Requests\UserUpdateRequest $request, $id) { User::findOrFail($id)->update($request->all()); return redirect("/backend/user")->with('message', 'The User has been updated'); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy(Requests\UserDestroyRequest $request, $id) { $user = User::findOrFail($id); $user->delete(); return redirect("/backend/user")->with('message', 'The User has been deleted'); } } |
Next, copy folder category inside views/backend folder and rename it as users folder.
index.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
@extends('layouts.backend.main') @section('title', 'Laravel 5 Blog | Users') @section('content') <div class="content-wrapper"> <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <div class="row mb-2"> <div class="col-sm-6"> <h1 class="m-0 text-dark">Users <small style="font-size: 15px">Display All Users</small></h1> </div><!-- /.col --> <div class="col-sm-6"> <ol class="breadcrumb float-sm-right"> <li class="breadcrumb-item"><a href="{{ url('/home') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li> <li class="breadcrumb-item"><a href="{{ url('/blog.index') }}">User</a></li> <li class="breadcrumb-item active">All Users</li> </ol> </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.container-fluid --> </div> <!-- /.content-header --> <!-- Main content --> <section class="content"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div class="card"> <!-- /.card-header --> <div class="card-header"> <h3 class="card-title">Users</h3> <div class="card-tools"> <div class="input-group input-group-sm" style="width: 150px;"> <input type="text" name="table_search" class="form-control float-right" placeholder="Search"> <div class="input-group-append"> <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button> </div> </div> </div> </div> <!-- /.card-header --> <div class="card-body p-1"> <div class="row"> <div class="col-md-6" style="padding-left: 10px; padding-right: 30px; padding-top: 10px; padding-bottom: 10px; "> <a href="{{ route('backend.user.create') }}" class="btn btn-info float-left"> <span> <i class="fa fa-plus-circle"></i> <span> Add User </span> </span> </a> </div> <div class="col-md-6" style="padding-left: 10px; padding-right: 30px; padding-top: 10px; padding-bottom: 10px; "> <div class="pull-right"> </div> </div> </div> @include('backend.partials.message') @if(!$users->count()) <div class="alert alert-danger"> <strong>No Record Found</strong> </div> @else @include('backend.user.table') @endif </div> <div class="card-footer clearfix"> <div class="pull-left" id="pagination"> {{ $users->appends( Request::query() )->render() }} </div> <div class="pull-right"> <?php $usersCount = $users->count();?> <small style="padding-right: 25px;">{{ $usersCount }} out of {{ $usersCount }} {{ str_plural('Items', $usersCount) }}</small> </div> </div> </div> <!-- /.card --> </div> </div> <!-- /.row --> </div><!-- /.container-fluid --> </section> <!-- /.content --> </div> <!-- /.content-wrapper --> @endsection @section('script') <script type="text/javascript"> $('#pagination').addClass('no-margin pagination-sm'); </script> @endsection |
table.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<table class="table table-striped"> <tr> <th width="10%">Action </th> <th>Name</th> <th>Email</th> <th>Role</th> </tr> @foreach($users as $user) <tr> <td> {!! Form::open(['method' => 'DELETE', 'route' => ['backend.user.destroy', $user->id] ]) !!} <a href="{{ route('backend.user.edit', $user->id) }}" class="btn btn-sm btn-default"><i class="fa fa-edit"></i></a> @if($user->id == config('cms.default_user_id')) <button onclick="return false" type="submit" class="btn btn-sm btn-danger disabled"><i class="fa fa-times"></i></button> @else <button onclick="return confirm('Are you sure to delete?')" type="submit" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></button> @endif {!! Form::close() !!} </td> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> <td>-</td> </tr> @endforeach </table> |
config/cms.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php return [ 'image' => [ 'directory' => 'img', 'thumbnail' => [ 'width' => 250, 'height' => 170 ] ], 'default_category_id' => 7, 'default_user_id' => 1, ]; |
Let’s test it :
Continue to edit files :
users/create.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
@extends('layouts.backend.main') @section('title', 'Laravel 5 Blog | Add New User') @section('content') <div class="content-wrapper"> <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <div class="row mb-2"> <div class="col-sm-6"> <h1 class="m-0 text-dark">User <small style="font-size: 15px">Add New User</small></h1> </div><!-- /.col --> <div class="col-sm-6"> <ol class="breadcrumb float-sm-right"> <li class="breadcrumb-item"><a href="{{ url('/home') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li> <li class="breadcrumb-item"><a href="{{ route('backend.user.index') }}">User</a></li> <li class="breadcrumb-item active">Add new</li> </ol> </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.container-fluid --> </div> <!-- /.content-header --> <!-- Main content --> <section class="content"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> {!! Form::model($user, [ 'method' => 'POST', 'route' => 'backend.user.store', 'id' => 'user-form' ])!!} @include('backend.user.form') {!! Form::close() !!} </div> </div> <!-- /.row --> </div><!-- /.container-fluid --> </section> <!-- /.content --> </div> <!-- /.content-wrapper --> @endsection |
users/edit.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
@extends('layouts.backend.main') @section('title', 'Laravel 5 Blog | Edit New User') @section('content') <div class="content-wrapper"> <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <div class="row mb-2"> <div class="col-sm-6"> <h1 class="m-0 text-dark">Blog <small style="font-size: 15px">Edit User</small></h1> </div><!-- /.col --> <div class="col-sm-6"> <ol class="breadcrumb float-sm-right"> <li class="breadcrumb-item"><a href="{{ url('/home') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li> <li class="breadcrumb-item"><a href="{{ url('/blog.index') }}">Blog</a></li> <li class="breadcrumb-item active">Edit User</li> </ol> </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.container-fluid --> </div> <!-- /.content-header --> <!-- Main content --> <section class="content"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> {!! Form::model($user, [ 'method' => 'PUT', 'route' => ['backend.user.update', $user->id], 'id' => 'user-form' ])!!} @include('backend.user.form') {!! Form::close() !!} </div> </div> <!-- /.row --> </div><!-- /.container-fluid --> </section> <!-- /.content --> </div> <!-- /.content-wrapper --> @endsection |
form.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<div class="card"> <!-- /.card-header --> <div class="card-header"> <h3 class="card-title">Add New User</h3> <div class="card-tools"> <div class="input-group input-group-sm" style="width: 150px;"> <input type="text" name="table_search" class="form-control float-right" placeholder="Search"> <div class="input-group-append"> <button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button> </div> </div> </div> </div> <!-- /.card-header --> <div class="card-body p-1"> <div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}"> {!! Form::label('name') !!} {!! Form::text('name', null, ['class'=>'form-control']) !!} @if($errors->has('name')) <span class="badge badge-danger">{{ $errors->first('name') }}</span> @endif </div> <div class="form-group {{ $errors->has('email') ? 'has-error' : ''}}"> {!! Form::label('email') !!} {!! Form::text('email', null, ['class'=>'form-control']) !!} @if($errors->has('email')) <span class="badge badge-danger">{{ $errors->first('email') }}</span> @endif </div> <div class="form-group {{ $errors->has('password') ? 'has-error' : ''}}"> {!! Form::label('password') !!} {!! Form::password('password', ['class'=>'form-control']) !!} @if($errors->has('password')) <span class="badge badge-danger">{{ $errors->first('password') }}</span> @endif </div> <div class="form-group {{ $errors->has('password_confirmation') ? 'has-error' : ''}}"> {!! Form::label('password_confirmation') !!} {!! Form::password('password_confirmation', ['class'=>'form-control']) !!} @if($errors->has('password_confirmation')) <span class="badge badge-danger">{{ $errors->first('password_confirmation') }}</span> @endif </div> <div class="pull-left"> <button type="submit" class="btn btn-primary">{{ $user->exists ? 'Update' : 'Save'}}</button> <a href="{{ route('backend.user.index') }}" class="btn btn-default">Back</a> </div> </div> </div> <!-- /.card --> </div> |
The next step is to create three requests :
1 2 3 |
php artisan make:request UserStoreRequest php artisan make:request UserUpdateRequest php artisan make:request UserDestroyRequest |
App\Http\Requests\UserStoreRequest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class UserStoreRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required', 'email' => 'email|required|unique:users', 'password' => 'required|confirmed' ]; } } |
UserUpdateRequest.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class UserUpdateRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'name' => 'required', 'email' => 'email|required|unique:users,email,' .$this->route('user'), 'password' => 'required_with:password_confirmation|confirmed' ]; } } |
For the destroy method, we will take another approach. If the user has many posts, the administrator will have a choice whether to also delete the post from that user, or move the post to another user.
Modify UsersController.php and add confirm() method :
1 2 3 4 5 6 7 8 9 |
public function confirm(Requests\UserDestroyRequest $request, $id) { $user = User::findOrFail($id); $users = User::where('id', '!=', $user->id)->pluck('name','id'); return view("backend.user.confirm", compact('user','users')); } |
Add another route to routes/web.php
1 2 3 4 |
Route::get('/backend/user/confirm/{user}',[ 'uses' => 'Backend\UsersController@confirm', 'as' => 'backend.user.confirm' ]); |
Modify view users/table.blade.php on this part :
1 2 3 4 5 6 7 8 9 10 |
<td> <a href="{{ route('backend.user.edit', $user->id) }}" class="btn btn-sm btn-default"><i class="fa fa-edit"></i></a> @if($user->id == config('cms.default_user_id')) <button onclick="return false" type="submit" class="btn btn-sm btn-danger disabled"><i class="fa fa-times"></i></button> @else <a href="{{ route('backend.user.confirm', $user->id) }}" class="btn btn-sm btn-danger"><i class="fa fa-times"></i></a> @endif </td> |
Add confirm.blade.php inside views/user folder :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
@extends('layouts.backend.main') @section('title', 'Laravel 5 Blog | Delete Confirmation') @section('content') <div class="content-wrapper"> <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <div class="row mb-2"> <div class="col-sm-6"> <h1 class="m-0 text-dark">User <small style="font-size: 15px">Delete Confirmation</small></h1> </div><!-- /.col --> <div class="col-sm-6"> <ol class="breadcrumb float-sm-right"> <li class="breadcrumb-item"><a href="{{ url('/home') }}"><i class="fa fa-dashboard"></i> Dashboard</a></li> <li class="breadcrumb-item"><a href="{{ route('backend.user.index') }}">User</a></li> <li class="breadcrumb-item active"Delete Confirmation</li> </ol> </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.container-fluid --> </div> <!-- /.content-header --> <!-- Main content --> <section class="content"> <div class="container-fluid"> <div class="row"> <div class="col-md-12"> {!! Form::model($user, [ 'method' => 'DELETE', 'route' => ['backend.user.destroy', $user->id], ])!!} <p>User to Delete : ID # {{ $user->id }} : {{ $user->name }}</p> <p>What you want to do with the posts by this user ? </p> <p><input type="radio" name="delete_option" value="delete" checked> Delete All Posts</p> <p><input type="radio" name="delete_option" value="attribute"> Attribute content to : </p> {!! Form::select('selected_user', $users, null) !!} <br><br> <button type="submit" class="btn btn-danger">Confirm Deletion</button> <a href="{{ route('backend.user.index') }}" class="btn btn-default">Cancel</a> {!! Form::close() !!} </div> </div> <!-- /.row --> </div><!-- /.container-fluid --> </section> <!-- /.content --> </div> <!-- /.content-wrapper --> @endsection |
Run the application, you will be able to choose whether to delete the posts by deleted author or attribute the post to another author :
Modify destroy() method inside UsersController.php :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public function destroy(Requests\UserDestroyRequest $request, $id) { $user = User::findOrFail($id); $deleteOption = $request->delete_option; $selectedUser = $request->selected_user; if($deleteOption == "delete"){ $user->posts()->withTrashed()->forceDelete(); }elseif($deleteOption == "attribute"){ $user->posts()->update(['author_id' => $selectedUser]); } $user->delete(); return redirect("/backend/user")->with('message', 'The User has been deleted'); } |
Add authorize() method to UserDestroyRequest:
1 2 3 4 5 |
public function authorize() { return !($this->route('users') == config('cms.default_user_id')) || $this->route('users') == auth()->user()->id); } |
Github commit.