role listing
This commit is contained in:
23
app/Http/Controllers/RoleController.php
Normal file
23
app/Http/Controllers/RoleController.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Role;
|
||||
use Illuminate\View\View;
|
||||
|
||||
class RoleController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of roles.
|
||||
*/
|
||||
public function index(): View
|
||||
{
|
||||
$roles = Role::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'created_at']);
|
||||
|
||||
return view('role.index', [
|
||||
'roles' => $roles,
|
||||
]);
|
||||
}
|
||||
}
|
||||
9
app/Models/Role.php
Normal file
9
app/Models/Role.php
Normal file
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
#[Fillable(['name'])]
|
||||
class Role extends Model {}
|
||||
28
database/migrations/2026_05_11_033145_create_roles_table.php
Normal file
28
database/migrations/2026_05_11_033145_create_roles_table.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('roles', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('roles');
|
||||
}
|
||||
};
|
||||
@@ -15,6 +15,9 @@
|
||||
<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||
{{ __('Dashboard') }}
|
||||
</x-nav-link>
|
||||
<x-nav-link :href="route('role.index')" :active="request()->routeIs('role.index')">
|
||||
{{ __('Roles') }}
|
||||
</x-nav-link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -70,6 +73,9 @@
|
||||
<x-responsive-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
|
||||
{{ __('Dashboard') }}
|
||||
</x-responsive-nav-link>
|
||||
<x-responsive-nav-link :href="route('role.index')" :active="request()->routeIs('role.index')">
|
||||
{{ __('Roles') }}
|
||||
</x-responsive-nav-link>
|
||||
</div>
|
||||
|
||||
<!-- Responsive Settings Options -->
|
||||
|
||||
42
resources/views/role/index.blade.php
Normal file
42
resources/views/role/index.blade.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<x-app-layout>
|
||||
<x-slot name="header">
|
||||
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
|
||||
{{ __('Roles') }}
|
||||
</h2>
|
||||
</x-slot>
|
||||
|
||||
<div class="py-12">
|
||||
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
|
||||
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
|
||||
<div class="p-6 text-gray-900">
|
||||
<div class="overflow-x-auto">
|
||||
<table class="min-w-full divide-y divide-gray-200">
|
||||
<thead class="bg-gray-50">
|
||||
<tr>
|
||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
|
||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Name</th>
|
||||
<th scope="col" class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Created</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-gray-200">
|
||||
@forelse ($roles as $role)
|
||||
<tr class="odd:bg-white even:bg-gray-50">
|
||||
<td class="px-4 py-3 text-sm text-gray-700">{{ $role->id }}</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-900">{{ $role->name }}</td>
|
||||
<td class="px-4 py-3 text-sm text-gray-700">{{ $role->created_at?->format('Y-m-d H:i') }}</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="3" class="px-4 py-6 text-center text-sm text-gray-500">
|
||||
No roles found.
|
||||
</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</x-app-layout>
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\ProfileController;
|
||||
use App\Http\Controllers\RoleController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', function () {
|
||||
@@ -12,6 +13,7 @@ Route::get('/dashboard', function () {
|
||||
})->middleware(['auth', 'verified'])->name('dashboard');
|
||||
|
||||
Route::middleware('auth')->group(function () {
|
||||
Route::get('/role', [RoleController::class, 'index'])->name('role.index');
|
||||
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
|
||||
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
|
||||
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
|
||||
|
||||
23
tests/Feature/RoleIndexTest.php
Normal file
23
tests/Feature/RoleIndexTest.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
|
||||
test('role index requires authentication', function () {
|
||||
$this->get('/role')->assertRedirect('/login');
|
||||
});
|
||||
|
||||
test('authenticated users can view the role index', function () {
|
||||
Role::query()->create(['name' => 'Admin']);
|
||||
Role::query()->create(['name' => 'Editor']);
|
||||
|
||||
$response = $this
|
||||
->actingAs(User::factory()->create())
|
||||
->get('/role');
|
||||
|
||||
$response
|
||||
->assertSuccessful()
|
||||
->assertSee('Roles')
|
||||
->assertSee('Admin')
|
||||
->assertSee('Editor');
|
||||
});
|
||||
Reference in New Issue
Block a user