在类别中从数据库表计数他的项目

时间:2015-12-14 02:32:09

标签: php laravel-5.1

我的型号代码

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

<?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();
    }

}

我的view.blade.php代码

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

@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

我的BasicController代码

public function grid(Request $request, $id)
    {  
        if ($id == 1) {
            $r = DB::table('sub_category')->select('*')->where('cat_id', $id)
            ->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();

            $name = DB::table('category')->where('cat_id', $id)->get();

           return view('buy-and-sell/grid', compact('r','name','count'));
        }

图片寻求帮助

for help

图片寻求帮助

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

2 个答案:

答案 0 :(得分:0)

我认为你在错误的地方关闭了PHP标签。改为:

<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>

所有PHP代码都需要保持在<?php?>

之间

显示你的图像显然是打印$ count然后它打印纯文本,所以你有80$data = ...

答案 1 :(得分:0)

如果您使用下面的代码计算控制器中的行数

$count = DB::table('products')->where('sub_id', $id)->count();

然后为什么在html中编写如此多的代码进行计数?

html中删除所有代码表单,然后使用此{{ $count }}