前回までの状況はこちら
最新ソースはこちら(gitHub)
https://github.com/takishita2nd/hotel-mng
今回はユーザーアカウントでログインした状態で予約登録する場合を作成していきます。
予約登録画面。
今までは宿泊者の情報を入力していますが、今回の仕様変更で、現在ログインしているユーザーの情報を表示させます。
<table class="edit">
<tr>
<th>名前</th>
<td>{!! $user->name !!}</td>
</tr>
<tr>
<th>住所</th>
<td>{!! $user->address !!}</td>
</tr>
<tr>
<th>電話番号</th>
<td>{!! $user->phone !!}</td>
</tr>
<tr>
<th>人数</th>
<td>{!! Form::select('num', ['1' => 1, '2' => 2]) !!}</td>
</tr>
<tr>
<th>宿泊部屋</th>
<td>{!! Form::select('room', $rooms) !!}</td>
</tr>
<tr>
<th>宿泊日数</th>
<td>{!! Form::number('days', 1) !!}</td>
</tr>
<tr>
<th>宿泊日</th>
<td>{!! Form::date('start_day', \Carbon\Carbon::now()) !!}</td>
</tr>
<tr>
<th>チェックアウト</th>
<td>{!! Form::select('checkout', $timelist) !!}</td>
</tr>
</table>
/**
* 入力フォーム
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('register.create',
[
'user' => Auth::user(),
'rooms' => $this->roomRepository->getRoomList(),
'timelist' => $this->registerManagement->getTimeList()
]
);
}
ログイン中のユーザー情報はAuth::user()で簡単に取り出すことができます。
予約登録処理。
まずは、バリデーションルールを修正します。
public function rules()
{
return [
'num' => 'required|numeric|digits_between:1,2',
'room' => 'required|numeric',
'days' => 'required|numeric|digits_between:1,4',
'start_day' => 'required|date',
];
}
登録するデータを変更します。
class RemoveCalumnReserveManagementTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('reserve_managements', function (Blueprint $table) {
$table->dropColumn('name');
$table->dropColumn('address');
$table->dropColumn('phone');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('reserve_managements', function (Blueprint $table) {
$table->string('name')->befor('num');
$table->string('address')->after('name');
$table->string('phone')->after('address');
});
}
}
class AddReserveManagementUserTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('reserve_management_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('reserve_management_id')
->foreign('reserve_management_id')
->references('id')->on('reserve_managements')
->onDelete('cascade');
$table->integer('user_id')
->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
$table->timestamps();
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('reserve_management_user');
}
}
private $paramNames = ['num', 'days', 'start_day', 'lodging', 'checkout'];
予約データとユーザーを結びつけます。
データベースの構成はこんな感じになります。
public function add($param, $room, $user)
{
$model = new ReserveManagement;
foreach($this->paramNames as $name)
{
$model->$name = $param[$name];
}
$model->save();
$this->attachToRoom($model, $room);
$this->attachToSchedule($model);
$this->attachToUser($model, $user);
}
public function attachToUser($model, $user)
{
$model->users()->attach($user);
}
public function detachToUser($model, $user)
{
$model->users()->detach($user);
}
public function store(ManagementRequest $request)
{
if($this->registerManagement->checkSchedule($request->start_day,
$request->days,
$request->room) == false)
{
return redirect('management/create')
->with(['error' => 'スケジュールが重複します'])
->withInput();
}
$param = $this->registerManagement->getParam();
$this->registerManagement->add([
$param[0] => $request->num,
$param[1] => $request->days,
$param[2] => $request->start_day,
$param[3] => false,
$param[4] => date('Y-m-d H:i', strtotime($request->start_day.'+'.$request->days.' day') + $request->checkout)
], $request->room, Auth::user());
return redirect('management');
}
最後に、登録した情報を予約一覧に表示させます。
ユーザーでログインしているときは、他のユーザーの情報が見えないようにしないといけません。
public function getList()
{
$select = ['reserve_managements.id as id', 'num', 'rooms.id as roomid', 'rooms.name as room', 'days', 'checkout', 'start_day'];
return ReserveManagement::select($select)
->where('lodging', false)
->orderBy('start_day')
->leftJoin('reserve_management_room', 'reserve_managements.id', '=', 'reserve_management_room.reserve_management_id')
->leftJoin('rooms', 'reserve_management_room.room_id', '=', 'rooms.id')
->get();
}
public function getListByMonth($year, $month, $room, $userId)
{
$select = ['reserve_managements.id as id', 'users.name as name', 'users.address as address', 'users.phone as phone', 'num', 'rooms.id as roomid', 'rooms.name as room', 'days', 'checkout', 'start_day'];
return ReserveManagement::select($select)
->leftJoin('reserve_management_room', 'reserve_managements.id', '=', 'reserve_management_room.reserve_management_id')
->leftJoin('rooms', 'reserve_management_room.room_id', '=', 'rooms.id')
->leftJoin('reserve_management_user', 'reserve_managements.id', '=', 'reserve_management_user.reserve_management_id')
->leftJoin('users', 'reserve_management_user.user_id', '=', 'users.id')
->where('start_day', '>=', date('Y-m-d', strtotime('first day of '.$year.'-'.$month)))
->where('start_day', '<=', date('Y-m-d', strtotime('last day of '.$year.'-'.$month)))
->where('reserve_management_room.room_id', $room)
->where('lodging', false)
->where('users.id', $userId)
->orderBy('start_day')
->get();
}
public function registers(Request $request)
{
return response()->json(['registerLists' => $this->registerManagement->getListByMonth(
$request->year,
$request->month,
$request->room,
Auth::user()->id
)]);
}
まだ十分ではないけれど、そこそこ形になってきました。