【问题标题】:How to fix duplicated checkboxes problem in laravel blade如何修复laravel刀片中的重复复选框问题
【发布时间】:2019-04-24 00:05:52
【问题描述】:

我正在开发巴士预订系统,我正在尝试将预订的座位详细信息传递给刀片,但我无法传递值

收集:

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::whereColumn('schedules_id', 'schedules_id')->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>

在这里,我使用了两个 froeach 第一个:@foreach($seat as $key =&gt; $item) 第二个:@foreach($bookingSeet as $seer)

这里,第一个拥有来自bus table的所有席位,49个席位与此绑定。

第二个是已预订的座位,来自booking table,然后与第一个核对,然后显示所有已预订的座位

预订表:

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

问题:问题是当我的 booking table 刀片中有 3 排时,我的刀片显示像这样的座椅 bus table seats = 49*3 = booking table no of rows 如果我有 4 排,那么 49*4

bookingseet 数组

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

bookingseet 一个数组看起来像这样

Collection {#418 ▼
  #items: array:3 [▼
    0 => Bookings {#432 ▼
      #fillable: array:7 [▼
        0 => "buses_id"
        1 => "users_id"
        2 => "schedules_id"
        3 => "routes_id"
        4 => "seat"
        5 => "price"
        6 => "profile"
      ]
      #primaryKey: "bookings_id"
      #connection: "mysql"
      #table: null
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #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" => "pending"
        "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) "pending" ["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) "pending" ["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) "*" } } } }

【问题讨论】:

  • 您应该在控制器中循环遍历 foreach 循环并生成一个关联数组,其中包含用于识别预订座位的键,以便在刀片上您需要循环一次,同时仅检查预订座位。我希望你能理解。
  • @DineshSuthar 对不起先生,我不太明白你的回答,哪个 foreach 循环?

标签: php laravel


【解决方案1】:

问题是您的第二个 foreach 正在制作重复的行。

尝试将第二个foreach 限制在您需要的地方。

@foreach($seat as $key => $item)

   <div class="col-md-1">
   <div class="seats back seats 
   @foreach($bookingSeet as $seer)
      @if(in_array($item['name'], $seer['seat']))
      activeSeat
      @endif"
   @endforeach
   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

【讨论】:

  • 试过了,先生,但只有一个座位显示为已预订,只有第一个
  • @thush 你能发一个var_dumpbookingSeet-&gt;toArray() 吗?
猜你喜欢
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
相关资源
最近更新 更多