嗨,我有这个选择复选框,这是当我检查所有的值是否通过它的时候!现在我的问题是,当我保存到数据库中时,它只保存了一个值。这是我的HTML
<p>Sunday</p>
<input type="checkbox" name="select-all-sunday" id="select-all-sunday" onclick="toggle(this);"> Check All
<br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-1" value="00:00"> 00:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-2" value="1:00"> 1:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-3" value="2:00"> 2:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-4" value="3:00"> 3:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-5" value="4:00"> 4:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-6" value="5:00"> 5:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-7" value="6:00"> 6:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-8" value="7:00"> 7:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-9" value="8:00"> 8:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-10" value="9:00"> 9:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-11" value="10:00"> 10:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-12" value="11:00"> 11:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-13" value="12:00"> 12:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-14" value="13:00"> 13:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-15" value="14:00"> 14:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-16" value="15:00"> 15:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-17" value="16:00"> 16:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-18" value="17:00"> 17:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-19" value="18:00"> 18:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-20" value="19:00"> 19:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-21" value="20:00"> 20:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-22" value="21:00"> 21:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-23" value="22:00"> 22:00 <br>
<input type="checkbox" name="checkbox-sunday[]" id="checkbox-24" value="23:00"> 23:00 <br>这现在是我的控制器
$sundays = $request->input('checkbox-sunday');
foreach($sundays as $sunday){
echo $sunday.",";
}现在,回显$sunday似乎正确显示了从表单传递的值。现在,在保存到数据库中时,只保存了一个值。
下面是我保存到数据库时的代码
$sundays = $request->input('checkbox-sunday');
foreach($sundays as $sunday){
echo $sunday.",";
$postRoom = PostRoom::find($id);
$postRoom->description = $request->get('description');
$postRoom->checkin_date = $request->get('bookingDate');
$postRoom->checkin_time = $request->get('checkinTime');
$postRoom->room_price = $request->get('roomPrice');
$postRoom->day_sunday = $sunday;
$postRoom->your_neighbourhood = $request->get('yourNeighbourhood');
$postRoom->save();
return redirect('add-your-listing/next-step-3/'.$postRoom->id);
}什么是拯救这个的最好方法?有人能帮帮我吗?蒂娅。
发布于 2019-10-22 23:11:54
通常你在你的循环中花费,你每次查找id并保存当前的给出循环,所以你粉碎它,最好的事情是制作一个表,并在这个字段中发送这个表(对不起,我的英语)
我试试这个。
$sundays = $request->input('checkbox-sunday');
$sundaysArray = array();
foreach($sundays as $sunday){
$sundaysArray[] = $sunday;
}
$postRoom = PostRoom::find($id);
$postRoom->description = $request->get('description');
$postRoom->checkin_date = $request->get('bookingDate');
$postRoom->checkin_time = $request->get('checkinTime');
$postRoom->room_price = $request->get('roomPrice');
$postRoom->day_sunday = json_encode($sundaysArray);
$postRoom->your_neighbourhood = $request->get('yourNeighbourhood');
$postRoom->save();
return redirect('add-your-listing/next-step-3/'.$postRoom->id);发布于 2019-10-23 00:17:23
您的问题基于数组到字符串的转换,您可以使用json_encode()或json_decode()在保存和读取时转换数组,或者对我来说,我使用serialize()和unserialize()以字符串数据类型存储和检索数据库中的数组集。
//read the checkbox input from the form.
$sundaysArray = $request->input('checkbox-sunday');
//while saving
$postRoom->day_sunday = json_encode($sundaysArray) //or
$postRoom->day_sunday = serialize($sundaysArray);
//while reading from database
$sundaysArray = json_decode($postRoom->day_sunday) //or
$sundaysArray = unserialize($postRoom->day_sunday);无论哪种方式,这都是可行的。
发布于 2019-10-22 23:15:09
模型PostRoom
protected $fillable = [
'bookingDate', 'description', 'checkinTime', 'roomPrice', 'day_sunday', 'your_neighbourhood'];代码
$sundays = $request->input('checkbox-sunday');
$data = [];
$hours = []
foreach($sundays as $sunday){
$hours[] = $sunday;
}
$room = PostRoom::find($id);
$data['description'] = $request->get('description');
$data['bookingDate'] = $request->get('bookingDate');
$data['checkinTime'] = $request->get('checkinTime');
$data['roomPrice'] = $request->get('roomPrice');
$data['day_sunday'] = implode(', ' $hours);
$data['your_neighbourhood'] = $request->get('yourNeighbourhood');
$room->update($data);
return redirect('add-your-listing/next-step-3/'.$room->id);https://stackoverflow.com/questions/58507147
复制相似问题