Создадим контроллер категорий:
php artisan make:controller Admin/CategoryController -r
Флаг -r обозначает, что контролер ресурсный (будут объявлены методы: index, create, store, show, edit, update, destroy).
Удалим метод show из созданного контроллера app/Http/Controllers/Admin/CategoryController.php , так как он нам не нужен.
Чтобы посмотреть все маршруты выполним команду:
php artisan route:list
Создадим ресурсный контроллер в routes/web.php :
Route::resource('categories', 'CategoryController');
В app/Http/Controllers/Admin/CategoryController.php в методе index запишем:
public function index()
{
$categories = Category::paginate(2);
return view('admin.categories.index', compact('categories'));
}
И создадим вид для показа категорий resources/views/admin/categories/index.blade.php:
@extends('admin.layouts.layout');
@section('content')
Категории
@endsection
Страница должна открываться по адресу http://laratest2.loc/admin/categories.
В нашу секцию можно заносить любые элементы из исходной темы AdminLTE 3 . Перейдем на страницу https://adminlte.io/themes/v3/index2.html и просто скопируем исходный код любого элемента.
Изменим файл resources/views/admin/categories/index.blade.php :
@extends('admin.layouts.layout');
@section('content')
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Категории</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Главная</a></li>
</ol>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<!-- Main content -->
<section class="content">
<a href="{{ route('categories.create') }}">
<button type="button" class="btn btn-primary">Добавить категорию</button>
</a>
@if (!empty($categories->count()))
<div class="card-body">
<table class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">id</th>
<th>Title</th>
<th>Slug</th>
<th style="width: 50px">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $category)
<tr>
<td>{{ $category->id }}.</td>
<td>{{ $category->title }}</td>
<td>{{ $category->slug }}</td>
<td style="display: flex;">
<a href="{{ route('categories.edit', ['category' => $category->id]) }}">
<button type="submit" class="btn btn-success btn-sm mr-2"><i
class="fas fa-edit"></i>
</button>
</a>
<form method="post"
action="{{ route('categories.destroy', ['category' => $category->id]) }}">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-danger btn-sm"
onclick="return confirm('Подтвердите удаление!')"><i
class="fas fa-trash-alt"></i></button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
{{ $categories->links() }}
</div>
@else
<div>Категорий пока нет!</div>
@endif
</section>
<!-- /.content -->
@endsection
Так же вниз файла resources/views/admin/layouts/layout.blade.php добавим скрипт, для того, чтобы список категорий не схлопывался:
$('.nav-sidebar a').each(function() {
let location = window.location.protocol + '//' + window.location.host + window.location.pathname;
let link = this.href;
if (link === location) {
$(this).addClass('active');
$(this).closest('.has-treeview').addClass('menu-open');
}
})