【问题标题】:Bootstrap/Tempalte not working when nested foreach is inserted in View using Laravel使用 Laravel 在视图中插入嵌套的 foreach 时,引导程序/模板不起作用
【发布时间】:2019-12-17 13:33:00
【问题描述】:

我的程序中有两个表。第一个是tbfininst,另一个是tbsubmissiontbfininst 的主键为fld_code,而tbsubmission 的外键为CODCODE。这是一对多的关系。 现在,我在我的blade.php 中显示tbfininst 的行。它看起来像这样。

然后,在 Submission (Periodicity) 列中,我想插入 tbsumission 中有多少行 tbfininst 有。顺便说一句,数据库在我开始之前就已经创建好了。所以我认为我不能使用模型,或者我可以吗?

无论如何,在我的控制器中,我尝试使用以下代码:

public function index()
{
   $fininst = DB::connection('portaldb')->table('tbfininst')->groupBy('fld_code')->get();

   $periodicity = DB::connection('portaldb')->table('tbsubmission')->select('CODCODE')->get();

   return view('pages/compliance', ['fininst' => $fininst, 'periodicity' => $periodicity]);
}

在我的 Blade.php 中:

@foreach($fininst as $fin)
<tr>
   <td>
     <center>        
       {{ $fin->fld_code }}
     </center>
   </td>
   <td>{{ $fin->fld_name }}</td>
   <td>
     @if ($fin->fld_status == 4)
        <p>Compliant</p>
     @else
        <p>Non-Compliant</p>
     @endif
   </td>
   <td>
     @if ($fin->fld_ip || $fin->fld_twofactor)
        <p>Compliant</p>
     @else
        <p>Non-Compliant</p>
     @endif
   </td>
   <td>
     @foreach($periodicity as $p)
        @if ($fin->fld_code == $p->CODCODE)
           {{$fin->fld_code}}
        @else
        @endif
     @endforeach
   </td>
   <td></td>
   <td></td>
   <td></td>
   <td></td>
 </tr>
@endforeach

当我插入周期性的@foreach 时,我的blade.php 输出有点不起作用。

我不知道为什么我的模板不起作用。我前两天刚开始使用 laravel,还在学习中。我接受批评,它会帮助我提高。谢谢。

【问题讨论】:

  • 看来您需要一个增量变量,而不是输出找到的值。当它们相等时,使用像 count++ 这样的变量。然后循环后输出count的值。
  • @CommonKnowledge,您好,感谢您的评论。我需要输出值,是客户端需要的

标签: php laravel laravel-blade


【解决方案1】:

您只是在应该计算匹配值的地方显示匹配值并像这样显示它们

@foreach($fininst as $fin)
   @php $count = 0; @endphp
        @foreach($periodicity as $p)
            @if ($fin->fld_code == $p->CODCODE)
               @php ++$count; @endphp
            @else
            @endif
         @endforeach
    <tr>
       <td>
         <center>        
           {{ $fin->fld_code }}
         </center>
       </td>
       <td>{{ $fin->fld_name }}</td>
       <td>
         @if ($fin->fld_status == 4)
            <p>Compliant</p>
         @else
            <p>Non-Compliant</p>
         @endif
       </td>
       <td>
         @if ($fin->fld_ip || $fin->fld_twofactor)
            <p>Compliant</p>
         @else
            <p>Non-Compliant</p>
         @endif
       </td>
       <td>

               {{$count}}

       </td>
       <td></td>
       <td></td>
       <td></td>
       <td></td>
     </tr>
    @endforeach

【讨论】:

  • 感谢您的回答,计数有效,但是.. 我的刀片布局在我尝试时不起作用。然后我的命令提示符出现错误,提示 Allowed memory size of 134217728 bytes exhausted (tried to allocate 63410176 bytes) in D:\Web-Applications\Test\portal\vendor\symfony\http-foundation\Response.php on line 363 知道这是什么吗?非常感谢!
  • 您可以尝试编辑 php.ini 文件并增加 memory_limit = 2048M 。也别忘了重启phpserver
  • 我尝试编辑我的 xampp/php/php.ini 的 memory_limit,但没有任何反应。这个对吗?还是我错过了任何 php.ini?
  • 你改变了这个文件/etc/php/7.0/cli/php.ini
  • 我想我没有/etc/php/7.0/cli/php.ini,因为我在windows电脑上
猜你喜欢
  • 2018-06-07
  • 1970-01-01
  • 2013-08-31
  • 2012-12-16
  • 2017-07-03
  • 2021-04-03
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多