Tìm hiểu route + truyền dữ liệu từ controller tới view qua bài quản lý khách sạn- Lập trình Laravel
Yêu cầu như sau
1. Tạo file route đặt tên là hotel.php -> thực hiện đăng ký để laravel nhận route trên
2. File route hotel.php được thiết kế như sau
<?php
Route::group(['prefix' => '/hotel'], function () {
Route::get('/view', 'HotelController@showAll')->name('showAll');
Route::get('/detail', 'HotelController@showDetail')->name('showDetail');
});
-> Yêu cầu viết hoàn thiện các phần còn thiếu để chương trình không bị lỗi.
Yêu cầu:
1. route name (showAll) sẽ gọi tới view đặt tên là index.blade.php
Hàm showAll thực hiện tạo 1 danh sách dữ liệu fake phòng trong khách sạn (gồm các trường : mã phòng, loại phòng, tầng, giá tiền), tên khách san, địa chỉ khách sạn -> thực hiện gửi các thông tin trên từ controller sang view. Và hiển thị chúng trên view.
2. route name (showDetail) sẽ gọi tới view đặt tên là view.blade.php
trong hàm showDetail của HotelController. Cài đặt các biên : mã phòng, loại phọng, tâng, giá tiền -> thực hiện gửi dữ liệu sang view detail và hiển thị nó.
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Đỗ Huy Hòa [community,C2110L]](https://www.gravatar.com/avatar/6c8b5f18494c0fec915a19f71158fc68.jpg?s=80&d=mm&r=g)
Đỗ Huy Hòa
2022-06-08 05:50:28
#web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HotelController;
/*
|--------------------------------------------------------------------------
| 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::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/helloworld', function () {
return view('BT1685.test');
});
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/student', function () {
return view('BT1685.student');
});
Route::get('/ptb1/a/{a}/b/{b}', [App\http\Controllers\HomeController::class, 'testbt']);
// Route::group(['prefix' => '/hotel'], function () {
// Route::get('/view', [HotelController::class, 'showAll'])->name('showAll');
// Route::get('/detail', [HotelController::class, 'showDetail'])->name('showDetail');
// });
Route::prefix('hotel')->group(function () {
Route::get('/view', [HotelController::class, 'showAll'])->name('showAll');
Route::get('/detail', [HotelController::class, 'showDetail'])->name('showDetail');
});
#HotelController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HotelController extends Controller
{
private $name_hotel, $address, $room_id, $room_type, $price, $floor;
public function showAll()
{
$hotels = [];
$index = 0;
for ($i = 1; $i <= 5; $i++) {
$hotels[] = [
'name_hotel' => 'Rose Hotel - ' . $i,
'address' => 'Ha noi ' . $i,
'room_id' => 'Room' . $i,
'floor' => 'Floor' . $i,
'room_type' => 'Vip' . $i,
'price' => $i * 100 + 2 * $i . '$',
];
}
return view('hotels/index', compact('hotels', 'index'));
}
public function showDetail(Request $request)
{
$index = 0;
$hotels = [];
for ($i = 1; $i <= 5; $i++) {
$hotels[] = [
'room_id' => 'Room' . $i,
'room_type' => 'vip' . $i,
'floor' => 'Floor' . $i,
'price' => $i * 100 + 2 * $i . '$',
];
}
return view('hotels/view', compact('hotels', 'index'));
}
}
#index.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
<table class="table table-bordered">
<thead class="bg-info text-light">
<tr>
<th>No</th>
<th>Name</th>
<th>Address</th>
<th>Room ID</th>
<th>Room Style</th>
<th>Price</th>
<th>Floor</th>
</tr>
</thead>
<tbody>
@foreach ($hotels as $hotel)
<tr>
<td>{{ ++$index }}</td>
<td>{{ $hotel['name_hotel'] }}</td>
<td>{{ $hotel['address'] }}</td>
<td>{{ $hotel['room_id'] }}</td>
<td>{{ $hotel['room_type'] }}</td>
<td>{{ $hotel['price'] }}</td>
<td>{{ $hotel['floor'] }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
@endsection
#view.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
<table class="table table-bordered">
<thead class="bg-info text-light">
<tr>
<th>No</th>
<th>Room ID</th>
<th>Room Style</th>
<th>Floor</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach ($hotels as $hotel)
<tr>
<td>{{ ++$index }}</td>
<td>{{ $hotel['room_id'] }}</td>
<td>{{ $hotel['room_type'] }}</td>
<td>{{ $hotel['floor'] }}</td>
<td>{{ $hotel['price'] }}</td>
</tr>
@endforeach
</tbody>
</div>
</div>
</div>
</div>
</div>
@endsection
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-06-24 15:42:31
#index.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Index Page</title>
</head>
<body>
@foreach ($roomList as $room)
<p>{{$hotelName}}</p>
<p>{{$address}}</p>
<p>{{$room['roomId']}}</p>
<p>{{$room['type']}}</p>
<p>{{$room['floor']}}</p>
<p>{{$room['price']}}</p>
@endforeach
</body>
</html>
#view.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>View Page</title>
</head>
<body>
@foreach ($roomList as $room)
<p>{{$room['roomId']}}</p>
<p>{{$room['type']}}</p>
<p>{{$room['floor']}}</p>
<p>{{$room['price']}}</p>
@endforeach
</body>
</html>
#HotelController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HotelController extends Controller
{
function showAll(Request $request){
$roomList = [];
for ($i=0; $i <= 5; $i++) {
$room [] = {
$roomId => 'room'.$i;
$type => 'vippro';
$floor => $i;
$price => '1000$';
}
}
$hotelName = "Sky Garden";
$address = "TP HCM";
return view('hotel/index')->with([
'roomList' => $roomList,
'hotelName' => $hotelName,
'address' => $address
]);
}
function showDetail(Request $request){
$roomList = [];
for ($i=0; $i <= 5 ; $i++) {
$roomList [] = {
$roomId => 'room'.$i;
$type =>'vippro';
$floor => $i;
$price => '1000$';
}
}
return view('hotel/view')->with([
'roomList' => $roomList,
]);
}
}
#hotel.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => '/hotel'], function () {
Route::get('/view','App\Http\Controllers\HotelController@showAll')->name('showAll');
Route::get('/detail', 'App\Http\Controllers\HotelController@showDetail')->name('showDetail');
});
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-06-23 12:26:56
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HotelController extends Controller
{
public function showAll(Request $request){
$maphong ='12345';
$loaiphong = 'vip';
$tang='1';
$giatien='345';
$ten_ks ='AAA';
$diachi_ks='BBB';
return view('index')->with([
'maphong' => $maphong,
'loaiphong' => $loaiphong,
'tang' => $tang,
'giatien' => $giatien,
'ten_ks' => $ten_ks,
'diachi_ks' => $diachi_ks
]);
}
public function showDetail(){
$maphong ='12345';
$loaiphong = 'vip';
$tang='1';
$giatien='345';
return view('view')->with([
'maphong' => $maphong,
'loaiphong' => $loaiphong,
'tang' => $tang,
'giatien' => $giatien
]);
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>{{$maphong}}</p>
<p>{{$loaiphong}}</p>
<p>{{$tang}}</p>
<p>{{$giatien}}</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<p>{{$maphong}}</p>
<p>{{$loaiphong}}</p>
<p>{{$tang}}</p>
<p>{{$giatien}}</p>
<p>{{$ten_ks}}</p>
<p>{{$diachi_ks}}</p>
</body>
</html>
<?php
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => '/hotel'], function () {
Route::get('/view','App\Http\Controllers\HotelController@showAll')->name('showAll');
Route::get('/detail', 'App\Http\Controllers\HotelController@showDetail')->name('showDetail');
});
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-06-23 09:17:17
<?php
namespace App\Http\Controllers\Hotel;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HotelController extends Controller
{
public function showAll(Request $request)
{
$roomList = [];
for ($i=1; $i <= 10; $i++) {
$roomList[]=[
'code' => 'DAT' . $i,
'type' => 'expensive',
'floor' => $i,
'price' => $i*100 + 2*$i
];
}
$hotelName = "Nguyen Tien Dat";
$hotelAddress = "Ha Noi";
return view('hotel/index')->with([
'roomList' => $roomList,
'hotelName' => $hotelName,
'hotelAddress' => $hotelAddress
]);
}
public function showDetail(Request $request)
{
$room = [
'code' => 'DAT09',
'type' => 'expensive',
'floor' => 19,
'price' => 20000
];
$hotelName = "Nguyen Tien Dat";
$hotelAddress = "Ha Noi";
return view('hotel/view')->with([
'room' => $room,
'hotelName' => $hotelName,
'hotelAddress' => $hotelAddress,
]);
}
}
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-06-25 09:11:54
hotel.php
<?php
use Illuminate\Support\Facades\Route;
Route::group(['prefix' => '/hotel'], function () {
Route::get('/view', 'HotelController@showAll')->name('showAll');
Route::get('/detail', 'HotelController@showDetail')->name('showDetail');
});
index.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>Name's Hotel</th>
<th>Address</th>
<th>Room is empty</th>
<th ></th>
</tr>
</thead>
<tbody>
@foreach ($hotelList as $hotel)
<tr>
<td>{{$hotel['hotelName']}}</td>
<td>{{$hotel['address']}}</td>
<td>10</td>
<td> <button type="button" class="btn btn-info"> <a style="color: white" href="detail">Detail</a> </button>
</td>
</tr>
@endforeach
</tbody>
</table>
<a href="" class="btn btn-success"> Input </a>
</div>
</body>
</html>
view.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Type of Room</th>
<th>floor</th>
<th >price</th>
</tr>
</thead>
<tbody>
@foreach ($hotelList as $hotel)
<tr>
<td>{{$hotel['roomId']}}</td>
<td>{{$hotel['typeRoom']}}</td>
<td>{{$hotel['floor']}}</td>
<td> {{$hotel['price']}}</button>
</td>
</tr>
@endforeach
</tbody>
</table>
<a href="view" class="btn btn-success"> <= </a>
</div>
</body>
</html>
HotelController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HotelController extends Controller
{
//
function showAll(Request $request){
$hotelList = [];
for ($i=1; $i < 10; $i++) {
# code...
$hotelList[]=[
'roomId' => 'Room'.$i,
'typeRoom'=> ($i%2==0)?'vip':'nomal',
'floor' => ($i%2==0)?'2':'1',
'price' => '$10/day',
'hotelName' => 'hotel'.$i,
'address' => $i
];
}
return view('hotel.index')->with([
'hotelList' => $hotelList
]);
}
function showDetail(Request $request){
$hotelList = [];
for ($i=1; $i < 10; $i++) {
# code...
$hotelList[]=[
'roomId' => 'Room'.$i,
'typeRoom'=> ($i%2==0)?'vip':'nomal',
'floor' => ($i%2==0)?'2':'3',
'price' => '$10/day',
'hotelName' => 'hotel'.$i,
'address' => $i
];
}
return view('hotel.view')->with([
'hotelList' => $hotelList
]);
}
}