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

Posted

技术标签:

【中文标题】如何从循环中获取值到刀片 - laravel【英文标题】:How to get the values from the loop to the blade - laravel 【发布时间】:2019-09-11 07:00:07 【问题描述】:

我使用 for 循环来获取查询并能够存储在数组中。 eg: 的数组看起来像 i: 时的样子

dd($s);

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

现在有了这些每个数组,我有更多数据,我想在刀片的选择框中显示它。

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

我只想显示 1Block 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 个数组,Block A 和 Block K

所以选择框应该在第二个选择框中包含块 A 的所有值(即:1 到 49 )和块 K 的所有值(即:1 到 149 )。

我不太清楚如何在这里获取值,但至少我能够在控制器中获取值

**

完整的控制器代码

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);


**

**

查看.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】:

所以既然这是你的数组结构,你想做的就是为此使用 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 循环中要做什么。我认为要解决您为“Block K”获取 100 个数据而不是 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>

块引用

【讨论】:

谢谢@aceraven,我试过了,它工作正常。但是,我刚刚意识到一个问题。显示块 A 有 49 个,块 K 有 149 个数组的数组是错误的。这些来自循环中的数据库。并且有两组数组。块 A 和块 K。在块 A 中,我有 49 个,这是正确的,在块 K 中,我有 100 个。所以在视图中,我在第一个选择框中得到 49,但在第二个选择框中我得到哪个是块 K,我得到 149。我怎样才能在这个选项中只得到 100 个数组?块 K 从循环中的第 50 个数组开始。你能帮忙吗?谢谢。 我附上了输出的截图。 谢谢,我会试试的。 $v 是我正在获取的 sql 查询。我正在尝试在每个 Blocks 中编号。 rNum 是数字。 好吧,我使用 dd($s) 从控制器获得了所需的值;但是,视图中的循环仍未在两个选择框中显示正确的值(如屏幕截图所示)。 – Sarah Malik 10 分钟前 @SarahMalik,你能分享控制器中的整个代码吗?以便我可以进一步帮助您。

以上是关于如何从循环中获取值到刀片 - laravel的主要内容,如果未能解决你的问题,请参考以下文章

如何在刀片foreach循环中获取迭代次数

如何在刀片模板的 foreach 循环中访问 laravel 集合?

尝试在 foreach 循环中获取密钥以使用刀片工作

C语言中,如何实现从文件中读取数据(大量的数据)后,在循环体中使用,每次传递3个值到数组。

微信小程序中如何获取for循环的item相关值到JS页面的问题

Laravel - 无法使用刀片从多维数组中获取值