如何在laravel中循环一个数据

我有按 id 产品打印条码的页面


像这样 :


http://img1.mukewang.com/6184d1820001ea7f10670234.jpg

我希望页面显示 20 个条形码,如下图所示


http://img.mukewang.com/6184d18c0001f1de07540623.jpg

我只有 1 个条码数据,但我想在页面上显示多达 20 个条码……所以,当我打印一个条码时,我可以打印多达 20 个条码,而不仅仅是一个


我的控制器:


public function barcodes($id){

    $title = $this->title; 


    $products = Product::find($id);

    $vars = compact('products');


    $data = ['products' => $products];

    return view($title.'.barcodes',compact('data','products'));

}

刀片视图:


<div class="row">

   <div class="col-12">

      <div class="card-box">

         <div class="header-title"> 

            <a href="#" class="btn btn-info btn-sm" onclick="printDiv('printableArea')" >

            <i class="fa fa-print"></i>

            Print

            </a>

         </div>

         <div class="panel-body" id="printableArea">

            <div class="col-md-2" style="padding: 10px; border: 1px solid #adadad;display:inline-block;line-height:16px !important; " align="center">

               <p>{{$products->name}}</p>

               <?php echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG($products->code, "c128A",1,40,array(1,1,1), true) . '"   />'; ?>

               <br>

               <small style="font-size: 8px !important;"><b>{{$products->code}}</b></small>

               <p style="line-height: 12px !important; font-size: 8px !important;">

                  <b>Price: {{$products->sale_price}} </b>

               </p>

            </div>

         </div>

      </div>

   </div>

</div>

<script>

   function printDiv(divName) {

       var printContents = document.getElementById(divName).innerHTML;

       var originalContents = document.body.innerHTML;

       document.body.innerHTML = printContents;

       window.print();

       document.body.innerHTML = originalContents;

   }

</script>


慕工程0101907
浏览 248回答 3
3回答

狐的传说

这将遍历每个 $product&nbsp;@foreach ($products as $product)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-box">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="header-title">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" class="btn btn-info btn-sm" onclick="printDiv('printableArea')" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-print"></i>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Print&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="panel-body" id="printableArea">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-2" style="padding: 10px; border: 1px solid #adadad;display:inline-block;line-height:16px !important; " align="center">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>{{$products->name}}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG($products->code, "c128A",1,40,array(1,1,1), true) . '"&nbsp; &nbsp;/>'; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<small style="font-size: 8px !important;"><b>{{$products->code}}</b></small>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p style="line-height: 12px !important; font-size: 8px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>Price: {{$products->sale_price}} </b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; @endforeachhttps://laravel.com/docs/5.8/blade#loops

慕桂英3389331

您可以使用的一种方法是foreach 循环。在你的情况下,你可以做这样的事情。@foreach($products AS $product)&nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp;<div class="col-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="card-box">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="header-title">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#" class="btn btn-info btn-sm" onclick="printDiv('printableArea')" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="fa fa-print"></i>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Print&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="panel-body" id="printableArea">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-2" style="padding: 10px; border: 1px solid #adadad;display:inline-block;line-height:16px !important; " align="center">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p>{{$product->name}}</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<?php echo '<img src="data:image/png;base64,' . DNS1D::getBarcodePNG($product->code, "c128A",1,40,array(1,1,1), true) . '"&nbsp; &nbsp;/>'; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<small style="font-size: 8px !important;"><b>{{$product->code}}</b></small>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<p style="line-height: 12px !important; font-size: 8px !important;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>Price: {{$product->sale_price}} </b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; </div>@endforeachForeach构造提供了一种简单的方法来迭代数组。

慕慕森

我只有 1 个条码数据,但我想在页面上显示多达 20 个条码……所以,当我打印一个条码时,我可以打印多达 20 个条码,而不仅仅是一个使用@for@for ($i = 0; $i < 20; $i++)&nbsp; &nbsp; // put HTML for barcode here@endfor
打开App,查看更多内容
随时随地看视频慕课网APP