Laravel 5.5: 将隐藏表单输入的值发送到控制器
Laravel 5.5: Send value of hidden form input to controller
在我看来,我获得了所有可用空位,因此用户可以单击预订按钮来预订该空位。但是,我似乎无法找到一种方法来获取 correct 值(输入的 id),因此我可以将数据库中特定预订的状态设置为已预订。
index.blade.php
@if(count($slots) > 0)
<table class="table table-striped">
<tr>
<th>Date</th>
<th>Time</th>
<th></th>
</tr>
@foreach($slots as $slot)
<tr>
<td>{{$slot->date}}</td>
<td>{{$slot->time}}</td>
<td>
<input name="id" value="{{$slot->id}}" type="hidden"> THIS IS WHAT I WANT TO SEND
<button class="btn btn-primary pull-right" type="submit">Book</button>
</td>
</tr>
@endforeach
BookingsController.php
public function store(Request $request)
{
$booking = new Booking;
$booking->user_id = auth()->user()->id;
$booking->date_id = THIS IS WHAT I NEED;
DB::table('calendar')
->select('id','status')
->where('id', GET DATE ID)
->update(['status' => 1]);
$booking->save();
return redirect('/bookings')->with([
'success' => 'Booking successful!',
]);
}
使用请求对象检索您发送的参数:
$whatYouNeed = $request->id
(或一般$request->WhateverYouNamedYourField
)
编辑:这不仅仅与隐藏字段相关,它适用于任何类型的表单字段。
在存储函数中,您是类型提示请求。
store(Request $request)
第一个 Request 指的是请求处理程序。
所以如果你把这个放在你的之后。
$booking->date_id = $request->input('id')
这就是你的答案。
您正在请求来自请求输入的输入id
来自 docs
$request->all();
或
$request->get('filedName');
或
$request->fieldName;
或
$request->input('fieldName');
这些是获取输入的方法,包括隐藏的输入
在我看来,我获得了所有可用空位,因此用户可以单击预订按钮来预订该空位。但是,我似乎无法找到一种方法来获取 correct 值(输入的 id),因此我可以将数据库中特定预订的状态设置为已预订。
index.blade.php
@if(count($slots) > 0)
<table class="table table-striped">
<tr>
<th>Date</th>
<th>Time</th>
<th></th>
</tr>
@foreach($slots as $slot)
<tr>
<td>{{$slot->date}}</td>
<td>{{$slot->time}}</td>
<td>
<input name="id" value="{{$slot->id}}" type="hidden"> THIS IS WHAT I WANT TO SEND
<button class="btn btn-primary pull-right" type="submit">Book</button>
</td>
</tr>
@endforeach
BookingsController.php
public function store(Request $request)
{
$booking = new Booking;
$booking->user_id = auth()->user()->id;
$booking->date_id = THIS IS WHAT I NEED;
DB::table('calendar')
->select('id','status')
->where('id', GET DATE ID)
->update(['status' => 1]);
$booking->save();
return redirect('/bookings')->with([
'success' => 'Booking successful!',
]);
}
使用请求对象检索您发送的参数:
$whatYouNeed = $request->id
(或一般$request->WhateverYouNamedYourField
)
编辑:这不仅仅与隐藏字段相关,它适用于任何类型的表单字段。
在存储函数中,您是类型提示请求。
store(Request $request)
第一个 Request 指的是请求处理程序。 所以如果你把这个放在你的之后。
$booking->date_id = $request->input('id')
这就是你的答案。
您正在请求来自请求输入的输入id
来自 docs
$request->all();
或
$request->get('filedName');
或
$request->fieldName;
或
$request->input('fieldName');
这些是获取输入的方法,包括隐藏的输入