Foreach在blade.php中复制值

时间:2019-04-25 05:13:21

标签: php laravel

我正在尝试将预订的座位传递到刀片服务器上,但是我的foreach复制值/复选框,在这里我使用的两个foreach彼此依赖,所以我无法删除< / p>

第一个foreach-@foreach($seat as $key => $item)此循环绑定了来自bus_table的所有席位(49)

第二个foreach-@foreach($bookingSeat as $seer)此循环绑定了来自bookings_table的预订座位

然后与if condition进行核对,然后致电css以获得预定的座位

我的问题是: enter image description here

第一个foreach座位= 49 * 3 = bookings_table中的行数

收集:

public function booking(Request $request)
    {
        //river is used to pass important params with flow of it from page to page
        $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)->where('profile', 'booked')->get();

        $bookingSeat = $bookingSeat->map(function ($bookSeat) {
            $bookSeat->seat = explode(",", $bookSeat->seat);
            return $bookSeat;
        });

        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(in_array($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>

预订表:

bookings_id users_id schedules_id buses_id routes_id seat price profile
    1           1         6           1       3        1  Null  booked
    2           1         6           1       3        2  Null  booked
    3           1         6           1       3        3  null  booked

bookingseet数组

Collection {#418 ▼
  #items: array:3 [▼
    0 => Bookings {#432 ▶}
    1 => Bookings {#433 ▶}
    2 => Bookings {#434 ▶}
  ]
}

预订看到一个数组看起来像这样(删除了其他信息以减少空间)

Collection {#418 ▼
        #attributes: array:10 [▼
        "bookings_id" => 1
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => array:1 [▼
          0 => "1"
        ]
        "price" => null
        "profile" => "booked"
        "created_at" => "2019-04-09 00:00:00"
        "updated_at" => "2019-04-09 00:00:00"
      ]
      #original: array:10 [▶]
    }

Var_dump:

object(Illuminate\Database\Eloquent\Collection)#418 (1) { ["items":protected]=> array(1) { [0]=> object(App\Bookings)#98 (26) { ["fillable":protected]=> array(7) { [0]=> string(8) "buses_id" [1]=> string(8) "users_id" [2]=> string(12) "schedules_id" [3]=> string(9) "routes_id" [4]=> string(4) "seat" [5]=> string(5) "price" [6]=> string(7) "profile" } ["primaryKey":protected]=> string(11) "bookings_id" ["connection":protected]=> string(5) "mysql" ["table":protected]=> NULL ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(10) { ["bookings_id"]=> int(6) ["users_id"]=> int(1) ["schedules_id"]=> int(10) ["buses_id"]=> int(3) ["routes_id"]=> int(7) ["seat"]=> array(1) { [0]=> string(1) "1" } ["price"]=> NULL ["profile"]=> string(7) "booked" ["created_at"]=> string(19) "2019-04-23 00:00:00" ["updated_at"]=> string(19) "2019-04-23 00:00:00" } ["original":protected]=> array(10) { ["bookings_id"]=> int(6) ["users_id"]=> int(1) ["schedules_id"]=> int(10) ["buses_id"]=> int(3) ["routes_id"]=> int(7) ["seat"]=> string(1) "1" ["price"]=> NULL ["profile"]=> string(7) "booked" ["created_at"]=> string(19) "2019-04-23 00:00:00" ["updated_at"]=> string(19) "2019-04-23 00:00:00" } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } } } }

$ seat的一个数组:

array:49 [▼
  0 => array:4 [▼
    "style" => "position: relative;"
    "id" => 0
    "name" => "1"
    "profile" => ""
  ]

我尝试了很多方法,但是没有用,我真的很困扰这个问题

1 个答案:

答案 0 :(得分:1)

在您的控制器中创建一个数组

  public function booking(Request $request)
   {

    $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)- 
    >where('profile', 'booked')->get();

    $bookingSeets=[];

if(!empty($bookingSeat)){
     foreach($bookingSeat as $seer){
      $bookingSeets[$seer->seat]='booked';
     }
  }

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

}

现在将$bookingSeets发送到刀片文件

在刀片文件中

   <div class="bus">
  @foreach($seat as $key => $item)
 <div class="col-md-1">
  <div class="seats back seats  
  {{!empty($bookingSeets[$item['name']])?'activeSeat':''}}
     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
   </div>

注意:$ item ['name']需要没有席位