Compare commits
2 Commits
2a83373b28
...
ea511a00e6
| Author | SHA1 | Date | |
|---|---|---|---|
| ea511a00e6 | |||
| ff904abf49 |
77
app/Http/Controllers/Admin/HotelController.php
Normal file
77
app/Http/Controllers/Admin/HotelController.php
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Models\Hotel;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class HotelController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Показать список отелей.
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$hotels = Hotel::all();
|
||||||
|
return response()->view('admin/hotels/index', compact('hotels'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Показать форму создания отеля.
|
||||||
|
*/
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
return view('admin.hotels.create');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Сохранить новый отель.
|
||||||
|
*/
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'address' => 'nullable|string',
|
||||||
|
'phone' => 'nullable|string',
|
||||||
|
]);
|
||||||
|
|
||||||
|
Hotel::create($validated);
|
||||||
|
|
||||||
|
return redirect()->route('admin.hotels.index')->with('success', 'Отель добавлен!');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Показать форму редактирования отеля.
|
||||||
|
*/
|
||||||
|
public function edit(Hotel $hotel)
|
||||||
|
{
|
||||||
|
return view('admin.hotels.edit', compact('hotel'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Обновить отель.
|
||||||
|
*/
|
||||||
|
public function update(Request $request, Hotel $hotel)
|
||||||
|
{
|
||||||
|
$validated = $request->validate([
|
||||||
|
'name' => 'required|string|max:255',
|
||||||
|
'address' => 'nullable|string',
|
||||||
|
'phone' => 'nullable|string',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$hotel->update($validated);
|
||||||
|
|
||||||
|
return redirect()->route('admin.hotels.index')->with('success', 'Отель обновлён!');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Удалить отель.
|
||||||
|
*/
|
||||||
|
public function destroy(Hotel $hotel)
|
||||||
|
{
|
||||||
|
$hotel->delete();
|
||||||
|
|
||||||
|
return redirect()->route('admin.hotels.index')->with('success', 'Отель удалён!');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,8 @@ class HotelController extends Controller
|
|||||||
{
|
{
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
return Hotel::all();
|
$hotels = Hotel::all();
|
||||||
|
return view('admin.hotels.index', compact('hotels'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
|
|||||||
@@ -4,27 +4,21 @@ use Illuminate\Database\Migrations\Migration;
|
|||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
return new class extends Migration
|
class CreateHotelsTable extends Migration
|
||||||
{
|
{
|
||||||
/**
|
public function up()
|
||||||
* Run the migrations.
|
|
||||||
*/
|
|
||||||
public function up(): void
|
|
||||||
{
|
{
|
||||||
Schema::create('hotels', function (Blueprint $table) {
|
Schema::create('hotels', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->string('name');
|
$table->string('name');
|
||||||
$table->string('address');
|
$table->text('address')->nullable();
|
||||||
$table->text('description')->nullable();
|
$table->string('phone')->nullable();
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function down()
|
||||||
* Reverse the migrations.
|
|
||||||
*/
|
|
||||||
public function down(): void
|
|
||||||
{
|
{
|
||||||
Schema::dropIfExists('hotels');
|
Schema::dropIfExists('hotels');
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
@@ -17,7 +17,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
// Создать админа
|
// Админ
|
||||||
$admin = Admin::firstOrCreate([
|
$admin = Admin::firstOrCreate([
|
||||||
'email' => 'admin@hotels.ru',
|
'email' => 'admin@hotels.ru',
|
||||||
], [
|
], [
|
||||||
@@ -25,7 +25,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
'password' => Hash::make('password'),
|
'password' => Hash::make('password'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Создать отель
|
// Отель
|
||||||
$hotel = Hotel::create([
|
$hotel = Hotel::create([
|
||||||
'name' => 'Grand Hotel',
|
'name' => 'Grand Hotel',
|
||||||
'address' => '123 Main St',
|
'address' => '123 Main St',
|
||||||
|
|||||||
45
resources/views/admin/hotels/index.blade.php
Normal file
45
resources/views/admin/hotels/index.blade.php
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
@extends('admin.layout')
|
||||||
|
|
||||||
|
@section('content')
|
||||||
|
<div>
|
||||||
|
<h1>Отели</h1>
|
||||||
|
|
||||||
|
@if(session('success'))
|
||||||
|
<div class="success">
|
||||||
|
{{ session('success') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
<a href="{{ route('admin.hotels.create') }}" class="btn">Добавить отель</a>
|
||||||
|
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Название</th>
|
||||||
|
<th>Адрес</th>
|
||||||
|
<th>Телефон</th>
|
||||||
|
<th>Действия</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach($hotels as $hotel)
|
||||||
|
<tr>
|
||||||
|
<td>{{ $hotel->name }}</td>
|
||||||
|
<td>{{ $hotel->address ?? '-' }}</td>
|
||||||
|
<td>{{ $hotel->phone ?? '-' }}</td>
|
||||||
|
<td>
|
||||||
|
<a href="{{ route('admin.hotels.edit', $hotel) }}" class="btn" style="background: #666;">Редактировать</a>
|
||||||
|
<form action="{{ route('admin.hotels.destroy', $hotel) }}" method="POST" style="display: inline;">
|
||||||
|
@csrf
|
||||||
|
@method('DELETE')
|
||||||
|
<button type="submit" class="btn" style="background: #aaa;" onclick="return confirm('Удалить отель?')">
|
||||||
|
Удалить
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
@endforeach
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
@endsection
|
||||||
97
resources/views/admin/layout.blade.php
Normal file
97
resources/views/admin/layout.blade.php
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title>@yield('title', 'Админка')</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: sans-serif;
|
||||||
|
margin: 0;
|
||||||
|
padding: 20px;
|
||||||
|
background: #fafafa;
|
||||||
|
color: #333;
|
||||||
|
}
|
||||||
|
.container {
|
||||||
|
max-width: 1200px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
header {
|
||||||
|
background: #444;
|
||||||
|
color: white;
|
||||||
|
padding: 15px 20px;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
nav a {
|
||||||
|
color: white;
|
||||||
|
text-decoration: none;
|
||||||
|
margin-left: 20px;
|
||||||
|
}
|
||||||
|
main {
|
||||||
|
margin-top: 20px;
|
||||||
|
}
|
||||||
|
h1 {
|
||||||
|
color: #333;
|
||||||
|
border-bottom: 2px solid #ddd;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
}
|
||||||
|
.btn {
|
||||||
|
display: inline-block;
|
||||||
|
background: #888;
|
||||||
|
color: white;
|
||||||
|
padding: 8px 16px;
|
||||||
|
text-decoration: none;
|
||||||
|
margin-right: 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
.btn:hover {
|
||||||
|
background: #777;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
width: 100%;
|
||||||
|
border-collapse: collapse;
|
||||||
|
margin: 20px 0;
|
||||||
|
background: white;
|
||||||
|
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
padding: 12px;
|
||||||
|
text-align: left;
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
}
|
||||||
|
th {
|
||||||
|
background: #eee;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
tr:nth-child(even) {
|
||||||
|
background: #f9f9f9;
|
||||||
|
}
|
||||||
|
.success {
|
||||||
|
background: #e0e0e0;
|
||||||
|
padding: 10px;
|
||||||
|
margin: 10px 0;
|
||||||
|
border-left: 4px solid #999;
|
||||||
|
}
|
||||||
|
.error {
|
||||||
|
background: #ffdddd;
|
||||||
|
padding: 10px;
|
||||||
|
margin: 10px 0;
|
||||||
|
border-left: 4px solid #cc0000;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h2>Админка</h2>
|
||||||
|
<nav>
|
||||||
|
<a href="{{ route('admin.hotels.index') }}">Отели</a>
|
||||||
|
<a href="{{ route('admin.logout') }}">Выйти</a>
|
||||||
|
</nav>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main class="container">
|
||||||
|
@yield('content')
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
<style>
|
<style>
|
||||||
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; }
|
body { font-family: sans-serif; max-width: 400px; margin: 50px auto; }
|
||||||
input { width: 100%; padding: 8px; margin: 5px 0; }
|
input { width: 100%; padding: 8px; margin: 5px 0; }
|
||||||
button { width: 100%; padding: 10px; background: #42b983; color: white; border: none; cursor: pointer; }
|
button { width: 100%; padding: 10px; background: #404040; color: white; border: none; cursor: pointer; }
|
||||||
.error { color: red; }
|
.error { color: red; }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -9,10 +9,21 @@ Route::post('/login', [\App\Http\Controllers\Admin\AuthController::class, 'login
|
|||||||
->name('admin.login');
|
->name('admin.login');
|
||||||
|
|
||||||
Route::middleware('auth')->group(function () {
|
Route::middleware('auth')->group(function () {
|
||||||
Route::get('/hotels', function () {
|
Route::get('/hotels', function () {
|
||||||
return '<h1>Отели</h1><a href="/admin/logout">Выйти</a>';
|
return response()->view('admin.hotels.index');
|
||||||
})->name('admin.hotels.index');
|
})->name('admin.hotels.index');
|
||||||
|
|
||||||
Route::post('/logout', [\App\Http\Controllers\Admin\AuthController::class, 'logout'])
|
Route::post('/logout', [\App\Http\Controllers\Admin\AuthController::class, 'logout'])
|
||||||
->name('admin.logout');
|
->name('admin.logout');
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::middleware('auth')->group(function () {
|
||||||
|
Route::get('/hotels', [\App\Http\Controllers\Admin\HotelController::class, 'index'])->name('admin.hotels.index');
|
||||||
|
Route::get('/hotels/create', [\App\Http\Controllers\Admin\HotelController::class, 'create'])->name('admin.hotels.create');
|
||||||
|
Route::post('/hotels', [\App\Http\Controllers\Admin\HotelController::class, 'store'])->name('admin.hotels.store');
|
||||||
|
Route::get('/hotels/{hotel}/edit', [\App\Http\Controllers\Admin\HotelController::class, 'edit'])->name('admin.hotels.edit');
|
||||||
|
Route::put('/hotels/{hotel}', [\App\Http\Controllers\Admin\HotelController::class, 'update'])->name('admin.hotels.update');
|
||||||
|
Route::delete('/hotels/{hotel}', [\App\Http\Controllers\Admin\HotelController::class, 'destroy'])->name('admin.hotels.destroy');
|
||||||
|
|
||||||
|
Route::post('/logout', [\App\Http\Controllers\Admin\AuthController::class, 'logout'])->name('admin.logout');
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user