叶片视图中的模型函数调用

时间:2015-12-17 18:15:50

标签: php laravel-5.1

我的型号代码

我们如何在blade.php中调用此函数

byte[] byte_array=null;
    try {

        String directorio = BuscarCarpetaArchivosReport(context);

        File f = new File(directorio, report + ".jrxml");
        if (f.exists() && f.isFile()) {
            FileInputStream fis = new FileInputStream(f);
            //Compilo el JRXML
            JasperReport jasperReport = JasperCompileManager.compileReport(fis);


            Map parametrosAux=(Map)parametros.get("parametros");

            JasperPrint jasperPrint;

              jasperPrint = JasperFillManager.fillReport(jasperReport, parametrosAux, new JREmptyDataSource());

            JRPdfExporter Exporter= new JRPdfExporter();

            SimplePdfExporterConfiguration configuracio=new     SimplePdfExporterConfiguration();
            configuracio.setPdfaConformance(PdfaConformanceEnum.PDFA_1A);
            configuracio.setIccProfilePath(directorio+"sRGB_v4_ICC_preference.icc");
               configuracio.setPdfVersion(PdfVersionEnum.VERSION_1_6);

            List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
             jasperPrintList.add(jasperPrint);
            Exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintList));
            java.io.ByteArrayOutputStream outputStream=new java.io.ByteArrayOutputStream();
            Exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream) );
            Exporter.setConfiguration(configuracio);
            Exporter.exportReport();
            byte_array=outputStream.toByteArray();


        } else {

        }
    } catch (Exception excep) {

              Logger.getLogger(BusquedaDatos.class.getName()).log(Level.SEVERE, null, excep);

    }
    return byte_array;

我的view.blade.php代码

在foreach循环中计数或在所有类别中显示

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BasicModel extends Model
{
     public static function get_product_count($id){
        $query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
        print_r($query);
        return $query->row_array();
    }

}

我的BasicController代码

@foreach ($r as $row)
         <li class="grid-item type-rent">
         <div class="property-block"> 
           <a href="#" class="property-featured-image"> <img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> --> </a>
              <div class="property-info">
                 <h4><a href="#">{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</a></h4>
                 <span class="location">NYC</span>
                   <div class="price"><strong>Items</strong><span>

                              <!-- start count code from here --> 
                              $data = $this->BasicModel->count {{ ($row->sub_id) }}
                              echo $data['count'];
                             </span></div>
                  </div>

               <!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
         </div>
    </li>
@endforeach

图片寻求帮助

for help

图片寻求帮助

此图片中的问题请解决问题 for help

1 个答案:

答案 0 :(得分:1)

虽然没有好的做法在刀片中访问数据库(最好在控制器中执行此操作并传递数据),但您可以这样做:

 <div class="price"><strong>Products</strong>
<span>
    {{ BasicModel::where('sub_id', $row->sub_id)->count() }}
  </span>
</div>

它没有经过测试,但看看Eloquent文档,那里解释了count()方法。

更新:如果laravel会找到类BasicModel(我永远不会直接在刀片中访问Models,我在控制器中执行此操作并传递数据。)所以我可能需要使用完整的命名空间来编写它。最有可能{{ \App\BasicModel::where() }}