diff --git a/app/Http/Controllers/Admin/HotelController.php b/app/Http/Controllers/Admin/HotelController.php new file mode 100644 index 0000000..ce73f3b --- /dev/null +++ b/app/Http/Controllers/Admin/HotelController.php @@ -0,0 +1,77 @@ +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', 'Отель удалён!'); + } +} diff --git a/app/Http/Controllers/HotelController.php b/app/Http/Controllers/HotelController.php index e20c76b..37468a4 100644 --- a/app/Http/Controllers/HotelController.php +++ b/app/Http/Controllers/HotelController.php @@ -10,7 +10,8 @@ class HotelController extends Controller { public function index() { - return Hotel::all(); + $hotels = Hotel::all(); + return view('admin.hotels.index', compact('hotels')); } public function store(Request $request) diff --git a/database/migrations/2025_12_28_230043_create_hotels_table.php b/database/migrations/2025_12_28_230043_create_hotels_table.php index f0c22bd..596d980 100644 --- a/database/migrations/2025_12_28_230043_create_hotels_table.php +++ b/database/migrations/2025_12_28_230043_create_hotels_table.php @@ -4,27 +4,21 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration +class CreateHotelsTable extends Migration { - /** - * Run the migrations. - */ - public function up(): void + public function up() { Schema::create('hotels', function (Blueprint $table) { $table->id(); - $table->string('name'); - $table->string('address'); - $table->text('description')->nullable(); + $table->string('name'); + $table->text('address')->nullable(); + $table->string('phone')->nullable(); $table->timestamps(); }); } - /** - * Reverse the migrations. - */ - public function down(): void + public function down() { Schema::dropIfExists('hotels'); } -}; +} \ No newline at end of file diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index a05ad84..794895b 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,7 +17,7 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - // Создать админа + // Админ $admin = Admin::firstOrCreate([ 'email' => 'admin@hotels.ru', ], [ @@ -25,7 +25,7 @@ class DatabaseSeeder extends Seeder 'password' => Hash::make('password'), ]); - // Создать отель + // Отель $hotel = Hotel::create([ 'name' => 'Grand Hotel', 'address' => '123 Main St', diff --git a/resources/views/admin/hotels/create.blade.php b/resources/views/admin/hotels/create.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/resources/views/admin/hotels/edit.blade.php b/resources/views/admin/hotels/edit.blade.php new file mode 100644 index 0000000..e69de29 diff --git a/resources/views/admin/hotels/index.blade.php b/resources/views/admin/hotels/index.blade.php new file mode 100644 index 0000000..4abb142 --- /dev/null +++ b/resources/views/admin/hotels/index.blade.php @@ -0,0 +1,45 @@ +@extends('admin.layout') + +@section('content') +
+

Отели

+ + @if(session('success')) +
+ {{ session('success') }} +
+ @endif + + Добавить отель + + + + + + + + + + + + @foreach($hotels as $hotel) + + + + + + + @endforeach + +
НазваниеАдресТелефонДействия
{{ $hotel->name }}{{ $hotel->address ?? '-' }}{{ $hotel->phone ?? '-' }} + Редактировать +
+ @csrf + @method('DELETE') + +
+
+
+@endsection \ No newline at end of file diff --git a/resources/views/admin/layout.blade.php b/resources/views/admin/layout.blade.php new file mode 100644 index 0000000..944bf8b --- /dev/null +++ b/resources/views/admin/layout.blade.php @@ -0,0 +1,97 @@ + + + + + @yield('title', 'Админка') + + + +
+

Админка

+ +
+ +
+ @yield('content') +
+ + \ No newline at end of file diff --git a/resources/views/admin/login.blade.php b/resources/views/admin/login.blade.php index 98ab5e4..fec46c3 100644 --- a/resources/views/admin/login.blade.php +++ b/resources/views/admin/login.blade.php @@ -6,7 +6,7 @@ diff --git a/routes/admin.php b/routes/admin.php index 3407069..8a1f340 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -9,10 +9,21 @@ Route::post('/login', [\App\Http\Controllers\Admin\AuthController::class, 'login ->name('admin.login'); Route::middleware('auth')->group(function () { - Route::get('/hotels', function () { - return '

Отели

Выйти'; + Route::get('/hotels', function () { + return response()->view('admin.hotels.index'); })->name('admin.hotels.index'); Route::post('/logout', [\App\Http\Controllers\Admin\AuthController::class, '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'); }); \ No newline at end of file