🥰2.5 Resource Users (ok)
Last updated
Last updated
C:\xampp82\htdocs\testvn\routes\web.php
<?php
use App\Http\Controllers\BannerController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\UsersController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::group(['middleware' => 'get.menu'], function () {
Route::get('/', function () {
return view('dashboard.homepage');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::group(['middleware' => ['role:admin']], function () {
Route::get('/test-role-admin', function () {
echo "Hello Admin";
});
Route::resource('users',UsersController::class)->except(['create', 'store']);
});
});
C:\xampp82\htdocs\testvn\app\Http\Controllers\UsersController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UsersController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$you = auth()->user();
$users = User::all();
return view('users.index', compact('users', 'you'));
}
public function show($id)
{
$user = User::find($id);
return view('users.show', compact('user'));
}
public function edit($id)
{
$user = User::find($id);
return view('users.edit', compact('user'));
}
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'name' => 'required|min:1|max:256',
'email' => 'required|email|max:256'
]);
$user = User::find($id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->save();
$request->session()->flash('message', 'Successfully updated user');
return redirect()->route('users.index');
}
public function destroy($id)
{
$user = User::find($id);
if ($user) {
$user->delete();
}
return redirect()->route('users.index');
}
}
C:\xampp82\htdocs\testvn\resources\views\users\index.blade.php
@extends('layouts.main')
@section('main')
<div class="container-fluid">
<div class="animated fadeIn">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 col-xl-12">
<div class="card">
<div class="card-header">
<i class="fa fa-align-justify"></i>{{ __('Users') }}
</div>
<div class="card-body">
<table class="table table-responsive-sm table-striped">
<thead>
<tr>
<th>Username</th>
<th>E-mail</th>
<th>Roles</th>
<th>Email verified at</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->menuroles }}</td>
<td>{{ $user->email_verified_at }}</td>
<td>
<a href="{{ url('/users/' . $user->id) }}" class="btn btn-block btn-primary">View</a>
</td>
<td>
<a href="{{ url('/users/' . $user->id . '/edit') }}" class="btn btn-block btn-primary">Edit</a>
</td>
<td>
@if( $you->id !== $user->id )
<form action="{{ route('users.destroy', $user->id ) }}" method="POST">
@method('DELETE')
@csrf
<button class="btn btn-block btn-danger">Delete User</button>
</form>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
C:\xampp82\htdocs\testvn\resources\views\users\show.blade.php
@extends('layouts.main')
@section('main')
<div class="container-fluid">
<div class="animated fadeIn">
<div class="row">
<div class="col-sm-6 col-md-5 col-lg-4 col-xl-3">
<div class="card">
<div class="card-header">
<i class="fa fa-align-justify"></i> User {{ $user->name }}
</div>
<div class="card-body">
<h4>Name: {{ $user->name }}</h4>
<h4>E-mail: {{ $user->email }}</h4>
<a href="{{ route('users.index') }}" class="btn btn-block btn-primary">{{ __('Return') }}</a>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
C:\xampp82\htdocs\testvn\resources\views\users\edit.blade.php
@extends('layouts.main')
@section('main')
<div class="container-fluid">
<div class="animated fadeIn">
<div class="row">
<div class="col-sm-6 col-md-5 col-lg-4 col-xl-3">
<div class="card">
<div class="card-header">
<i class="fa fa-align-justify"></i> {{ __('Edit') }} {{ $user->name }}
</div>
<div class="card-body">
<br>
<form method="POST" action="/users/{{ $user->id }}">
@csrf
@method('PUT')
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">
<svg class="icon icon-sm">
<use xlink:href="/assets/icons/coreui/free-symbol-defs.svg#cui-user"></use>
</svg>
</span>
</div>
<input class="form-control" type="text" placeholder="{{ __('Name') }}" name="name"
value="{{ $user->name }}" required autofocus>
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">@</span>
</div>
<input class="form-control" type="text" placeholder="{{ __('E-Mail Address') }}" name="email"
value="{{ $user->email }}" required>
</div>
<button class="btn btn-block btn-success" type="submit">{{ __('Save') }}</button>
<a href="{{ route('users.index') }}" class="btn btn-block btn-primary">{{ __('Return') }}</a>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection