[英]model function call in blade view laravel
我们如何在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();
}
}
计入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
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'));
}
尽管在Blade中访问DB并不是一个好习惯(最好在控制器中执行此操作并传递数据),但您可以执行以下操作:
<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() }}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.