我如何在Laravel 8中编辑房间预订。控制器的更新不起作用
控制器(更新):您可以在更新中看到,我试图编辑房间预订(时间和日期),但一部分可以使用IF或其他!
public function update(Request $request, Roomreservation $roomreservation)
{
$request->validate([
'date' => 'required',
'time' => 'required',
]);
$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
if ($roomreservation->exists()) {
return back()->with('erreur', 'The chosen room is already reserved');
}
else {
$roomreservation->update([
"date" => $request->date,
"time" => $request->time,
]);
return back()->with('message_sent', 'room reservation edited successfully!');
}
}
我的表单
<form method="post" action="{{ route('admin.roomreservations.update', $roomreservation->id) }}">
@csrf
@method('PUT') @method('PUT')
<div>
<div>
<label for="date">Date</label>
<input type="date" min="2022-01-01" max="2022-12-31" name="date" id="date"
value="{{ old('date', $roomreservation->date) }}" />
</div>
<div>
<label for="time">Time</label>
<select name="time" id="time"
value="{{ old('time',$roomreservation->time) }}" >
<option>8H30-10H00</option>
<option>10H00-11H30</option>
<option>11H30-13H00</option>
</select>
</div>
<div>
<button>
Edit
</button>
</div>
</div>
</form>
索引表格上的按钮更新
<a href="{{ route('admin.roomreservations.edit', $roomreservation->id) }}" >Update</a>
我认为更新条件上有问题
$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
if ($roomreservation->exists())
controller(update) :as you can see in the update i'm trying to edit the room reservation(time and date),but onlly one part work the if or the else!
public function update(Request $request, Roomreservation $roomreservation)
{
$request->validate([
'date' => 'required',
'time' => 'required',
]);
$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
if ($roomreservation->exists()) {
return back()->with('erreur', 'The chosen room is already reserved');
}
else {
$roomreservation->update([
"date" => $request->date,
"time" => $request->time,
]);
return back()->with('message_sent', 'room reservation edited successfully!');
}
}
my form
<form method="post" action="{{ route('admin.roomreservations.update', $roomreservation->id) }}">
@csrf
@method('PUT') @method('PUT')
<div>
<div>
<label for="date">Date</label>
<input type="date" min="2022-01-01" max="2022-12-31" name="date" id="date"
value="{{ old('date', $roomreservation->date) }}" />
</div>
<div>
<label for="time">Time</label>
<select name="time" id="time"
value="{{ old('time',$roomreservation->time) }}" >
<option>8H30-10H00</option>
<option>10H00-11H30</option>
<option>11H30-13H00</option>
</select>
</div>
<div>
<button>
Edit
</button>
</div>
</div>
</form>
button update on the index form
<a href="{{ route('admin.roomreservations.edit', $roomreservation->id) }}" >Update</a>
i think something is wrong on the update condition
$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
if ($roomreservation->exists())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您传递了一个错误的参数
将您的更新功能更改为下面
It seems that you're passing a wrong parameter
Change your update function to the below