php-使用Laravel显示类别和最低价格
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-使用Laravel显示类别和最低价格,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1935字,纯文字阅读大概需要3分钟。
内容图文
![php-使用Laravel显示类别和最低价格](/upload/InfoBanner/zyjiaocheng/665/be3d60e2e5de4a7f83c3b2c4062567ed.jpg)
尝试显示数据库中的所有类别以及类别中基于产品的类别,以显示最低价格.所以我到目前为止是:
模型Categories.php
class Categories extends Eloquent {
protected $table = 'category';
protected $primaryKey = 'category_id';
public $timestamps = false;
public function products()
{
return $this->hasMany('Product', 'category_id');
//return $this->belongsToMany('Product', 'category_id');
}
}
这是Product.php模型
class Product extends Eloquent {
protected $table = 'products';
protected $primaryKey = 'product_id';
public function categories()
{
return $this->hasMany('Categories', 'category_id');
//return $this->belongsToMany('Categories', 'category_id');
}
public $timestamps = false;
}
这是HomeController.php,它加载index.blade.php视图
class HomeController extends BaseController {
public function index()
{
$products = Product::where('category_id', '!=', 1)
->with('category')
->min('price')
->get();
return View::make('site.index', [
'categories' => $categories
]);
}
}
现在,当我加载页面时,出现此错误
production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function get() on a non-object' in ....
这有什么问题,如何显示类别和每个类别的最低价格?
更新:这是现在的样子
Categories.php模型
class Categories extends Eloquent {
protected $table = 'category';
protected $primaryKey = 'category_id';
public $timestamps = false;
public function products()
{
return $this->hasMany('Product', 'category_id');
}
public function lowestProduct() {
return $this->products()->selectRaw('*, max(price) as aggregate')
->groupBy('products.product_id')->orderBy('aggregate');
}
}
HomeController.php
public function index()
{
$categories = Categories::with('lowestProduct')->get();
//$categories = Categories::paginate(15);
return View::make('site.index', [
'categories' => $categories
]);
}
解决方法:
在您看来,您可能有一个foreach语句.放在这里:
$category-> products()-> min(‘price’);
这是该类别中最低的产品价格.
内容总结
以上是互联网集市为您收集整理的php-使用Laravel显示类别和最低价格全部内容,希望文章能够帮你解决php-使用Laravel显示类别和最低价格所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。