如何将多个字符串值传递给复选框?

时间:2019-04-06 08:00:49

标签: php laravel

我正在开发一个公共汽车座位预订系统,问题是从称为“ booking”的“ seat”列表名称获取预订的座位值,这里所有值都使用implode(',',$ seat)方法插入因此,如果我将一个值传递给视图,它将显示预订的座位,但是如果传递多个字符串,它将不会显示。我知道这是一个字符串,所以还有其他方法可以做到吗?数组对我不起作用。

插入

$booking = new Bookings();
$booking->users_id = 4;
$booking->schedules_id = $schedules_id;
$booking->buses_id = $buses_id;
$booking->routes_id = $routes_id;
$booking->seat = implode(',', $seat);
$booking->price = $request->price;
$booking->profile = 'pending';

收集

$seat = $request->seat;
$buses_id = $request->buses_id;
$schedules_id = $request->schedules_id;
$data = Buses::where('buses_id', $buses_id)->first();
$seat = json_decode($data->seat_layout, true);
$front = json_decode($data->front_layout, true);
$bookingSeat = Bookings::where('schedules_id', $schedules_id)->get();

return view('frontend.booking', ['seat' => $seat, 'buses_id' => $buses_id, 'schedules_id' => $schedules_id, 'front' => $front, 'bookingSeet'=>$bookingSeat]);

blade.php

<div class="bus">
  @foreach($seat as $key => $item)
  @foreach($bookingSeet as $seer)
    <div class="col-md-1">
      <div class="seats back seats 
      @if($item['name'] == $seer['seat'])
      activeSeat
      @endif"
      data-id="{{$key}}">
        <div class="special-attributes"></div>
        @if(isset($item['name'])){{$item['name']}}@else 11A @endif

       <input type="checkbox" name="seat_id[]" id="{{$key}}" value="{{$key}}">

      </div>
    </div>
  @endforeach
  @endforeach
</div>

2 个答案:

答案 0 :(得分:0)

如果POST席位是一个数组,并且将其另存为爆破字符串,则

因此,在获取操作中,迭代所有预订座位并将其分解回数组

$bookingSeat = Bookings::where('schedules_id', $schedules_id)->get();
$bookingSeat = $bookingSeat->map(function($bookSeat) {
      $bookSeat->seat = explode(",", $bookSeat->seat);
      return $bookSeat;
});

然后在刀片中检查座位是否为in_array

@if(in_array($item['name'], $seer['seat']))

答案 1 :(得分:0)

您正在implode-将座位成串,但是在将它们传递到前端之前没有explode-将它们座位化。我假定,应将此行更改为按预期工作:

$bookingSeat = explode(',', Bookings::where('schedules_id', $schedules_id)->get());

但是我不知道为什么有两个循环而不是一个循环:

  1. 第一个循环应显示公交车上所有可用的座位(正确)
  2. 第二个循环遍历所有预订,但这是根据公共汽车上的每个可用座位来完成的(这是不必要的/不正确的)

相反,您应该检查座位是否已预留,并删除第二个回圈:

 @if(in_array($item['name'], bookingSeet))
     <input type="checkbox" name="seat_id[]" id="{{$key}}" value="{{$key}}" checked="checked">
 @else
     <input type="checkbox" name="seat_id[]" id="{{$key}}" value="{{$key}}">
 @endif