如何从循环到刀片获取值-Laravel

时间:2019-04-22 02:11:58

标签: php arrays laravel loops laravel-blade

我用于循环来获取查询并能够存储在数组中。例如:的数组看起来像当我:

  

dd($ s);

  array:2 [▼
  "Block A" => array:49 [▶]
  "Block K" => array:149 [▶]
]

现在有了这些数组,我有更多数据,我想将其显示在刀片​​服务器的选择框中。

array:2 [▼
"Block A" => array:49 [▼
0 => array:1 [▼
  "Block A0" => "1"
]

我只想显示 1 ,它位于 A0块=> 1

  

homeController.php

foreach ($v as $f ) {
    $m[$i] = [
            $l.$i => $f->rNum,
          ];

    $s[$l] = $m;
    $i++;
  }
 $s = (array_filter($s));

 return view('home')->with($s);
 // I have also tried with

 //return view('home')->with('res',$s);
  

Home.blade.php

@foreach ($res as $s)
    <option value="> {{_____?____}}" </option>
             //I only want to display 1  to 49 here
@endforeach

请记住,有2个数组,A块和K块

因此,选择框在第二个选择框中应具有Block A的所有值(即:1到49)和Block K的所有值(即:1到149)。

我不太确定如何在此处获取值,但至少我能够在控制器中获取值

enter image description here

**

  

完整的控制器代码

public function viewHall($id){

  $path = request()->path();

  $substring = substr($path, 0,10);

  $title;
  $viewApp;

  $viewroom = "";
  $s[] = array();

  $i = 1;
  if ($substring == "view/hall/"){
    $viewApp = DB::select('Select * from applications where hall = "'.$id .'" AND status = "pending"');

     foreach ($viewApp as $k) {
       $a[] = $k->flat;
}

        $countA = (count($a));

        foreach ($a as $l) {

                    $viewroom = DB::select('Select applications.*  , residences.*  , rooms.* from applications
                    LEFT Join residences on applications.resi = residences.resiName
                    LEFT JOIN rooms on rooms.resiID = residences.id
                    where hall = "'.$id .'" AND status =  "pending" AND rooms.available = 0
                    AND rooms.flatName ="'.$l.'" group by  rooms.flatName, rooms.roomNum');


                         foreach ($viewroom as $f ) {
                           $s[$l][$i] = [
                                      $l => $f->roomNum,
                                  ];

                                  $i++;


                         }


                  }

                  $s = (array_filter($s));





    $title = "Hall (" . $id .")";

    return view('admin.viewapp')->with('applications', $viewApp)->with('title',$title)->with('rid',$id)->with('rooms',$viewroom)
                                ->with('roomNs', $s)->with('flat',$a)->with('flatc', $m);
  }

  elseif ($substring == "view/resi/"){
    $viewApp = DB::select('Select * from applications where resi = "'.$id .'" AND status = "pending"');

    $viewroom = DB::select('Select applications.*  , residences.*  , rooms.* from applications
                           LEFT Join residences on applications.resi = residences.resiName
                           LEFT JOIN rooms on rooms.resiID = residences.id
                           where resi = "'.$id .'" AND status =  "pending" AND rooms.available = 0');
    $title = "Residence (" . $id .")";
  }
  elseif ($substring == "view/year/"){
    $viewApp = DB::select('Select * from applications where year = "'.$id .'" AND status = "pending"');

    $viewroom = DB::select('Select applications.*  , residences.*  , rooms.* from applications
                           LEFT Join residences on applications.resi = residences.resiName
                           LEFT JOIN rooms on rooms.resiID = residences.id
                           where applications.year = "'.$id .'" AND status =  "pending" AND rooms.available = 0');
    $title = "Year (" . $id .")";
  }
  else{
    $viewApp = DB::select('Select * from applications where status = "pending"');

    $viewroom = DB::select('Select applications.*  , residences.*  , rooms.* from applications
                           LEFT Join residences on applications.resi = residences.resiName
                           LEFT JOIN rooms on rooms.resiID = residences.id
                           where  status =  "pending" AND rooms.available = 0');
    $title = "View All";
  }

  return view('admin.viewapp')->with('applications', $viewApp)->with('title',$title)->with('rid',$id)->with('rooms',$viewroom)
                              -with('roomNs', $roomN);

}

**

**

  

View.blade.php

<form class="" action="{{URL('process/')}}" method="post">
        {{ csrf_field() }}
        <table class="table">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Hall</th>
              <th scope="col">Residence</th>
              <th scope="col">Year</th>
              <th scope="col">Flat</th>
              <th scope="col">Room</th>
              <th scope="col">Status</th>
              <th scope="col">Submitted on</th>
              <th scope="col">Approve</th>
              <th scope="col">Reject</th>
            </tr>
          </thead>
          <tbody>
            <tr>

              @foreach ($applications as $show)
              <input type="hidden" name="id" value="{{$show->id}}">
              <input type="hidden" name="hall" value="{{$show->hall}}">
              <input type="hidden" name="resID" value="{{$show->resID}}">
              <input type="hidden" name="year" value="{{$show->year}}">
              <input type="hidden" name="flat" value="{{$show->flat}}">
              <th scope="row">{{$i}}</th>
              <td>{{$show->hall}}</td>
              <td>{{$show->resi}}</td>
              <td>{{$show->year}}</td>
              <td>{{$show->flat}}</td>
              <td>


                <div class="form-group">
                   <select class="form-control" id="roomNo" name="roomNo" required autofocus>
                     <option value="" selected>Select Room Number</option>
                     <?php $i=0; ?>
                     @foreach ($roomNs as $key1 => $value1)
                             @foreach ($value1 as $key2 => $value2)
                                 @foreach ($value2 as $key3 => $value3)
                                     <option value="{{ $value3 }}"> {{ $value3 }} </option>
                                 @endforeach
                             @endforeach
                          @endforeach
                   </select>
                 </div>



              </td>
              <td>{{$show->status}}</td>
              <td>{{$show->created_at}}</td>
              <td><input type="submit" class="btn btn-primary"name="approve" value="Approve"></td>
              <td><input type="submit" class="btn btn-danger"name="reject" value="Reject"></td>
            </tr>

            @endforeach
          </tbody>
        </table>
        </form>

**

1 个答案:

答案 0 :(得分:2)

因此,既然这是您的数组结构,那么您要为此使用3个foreach循环。

假设在您的控制器中返回以下内容:

return view('home')->with('res', $s);

这是您要在视图中执行的操作:

@foreach ($res as $key1 => $value1)
    @foreach ($value1 as $key2 => $value2)
        @foreach ($value2 as $key3 => $value3)
            <option value="{{ $value3 }}"> {{ $key3 }}" </option>
        @endforeach
    @endforeach
@endforeach

PS

请正确命名变量。不要像$s$res这样命名,如果您适当地命名变量,它将在将来随着项目的发展而对您有很大帮助。

跟进答案:

在您的控制器中。我不确定$v变量的值是什么,以及您打算在foreach循环中做什么。我认为要解决有关获取100个“块K”数据而不是149个数据的问题,您必须执行以下操作:

foreach ($v as $f ) {
    $s[$l][$i] = [
        $l.$i => $f->rNum,
    ];

    $i++;
}

答案2:

这是我认为的建议:

<form class="" action="{{URL('process/')}}" method="post">
    {{ csrf_field() }}
    <table class="table">
        <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Hall</th>
                <th scope="col">Residence</th>
                <th scope="col">Year</th>
                <th scope="col">Flat</th>
                <th scope="col">Room</th>
                <th scope="col">Status</th>
                <th scope="col">Submitted on</th>
                <th scope="col">Approve</th>
                <th scope="col">Reject</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($applications as $show)
                <tr>
                    <input type="hidden" name="id" value="{{$show->id}}">
                    <input type="hidden" name="hall" value="{{$show->hall}}">
                    <input type="hidden" name="resID" value="{{$show->resID}}">
                    <input type="hidden" name="year" value="{{$show->year}}">
                    <input type="hidden" name="flat" value="{{$show->flat}}">
                    <th scope="row">{{$i}}</th>
                    <td>{{$show->hall}}</td>
                    <td>{{$show->resi}}</td>
                    <td>{{$show->year}}</td>
                    <td>{{$show->flat}}</td>
                    <td>


                        <div class="form-group">
                            <select class="form-control" id="roomNo" name="roomNo" required autofocus>
                                <option value="" selected>Select Room Number</option>
                                <?php $i=0; ?>
                                @foreach ($roomNs[$show->flat] as $key1 => $value1)
                                    @foreach ($value1 as $key2 => $value2)
                                        <option value="{{ $value2 }}"> {{ $value2 }} </option>
                                    @endforeach
                                @endforeach
                            </select>
                        </div>



                    </td>
                    <td>{{$show->status}}</td>
                    <td>{{$show->created_at}}</td>
                    <td><input type="submit" class="btn btn-primary"name="approve" value="Approve"></td>
                    <td><input type="submit" class="btn btn-danger"name="reject" value="Reject"></td>
                </tr>
            @endforeach
        </tbody>
    </table>
</form>
  

Blockquote