1

我想在两个表的页面数据上显示,但有一点条件,我不知道如何在 Laravel 4 中制作它。

所以我有 tablecategories和 table products。目前我在页面上显示所有类别,没有问题。问题是现在会有没有类别的产品,我也想在页面上循环它们。

当管理员创建产品时,他选择类别,但如果不选择任何类别,它将1默认保存在数据库productssingle_product中。

这是产品型号

public function categories()
{
    return $this->hasMany('Categories', 'category_id');
}

和类别模型

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');
}

这就是观点

    <div class="col-xs-8 text-left" style="margin-top: 9px">
        @if($category['no_category'] == 0)
            Price: <strong>{{ $category->products()->min('price') }} $</strong>                         
        @else
            Price from: <strong>{{ $category->products()->min('price') }} $</strong> 
         @endif
    </div>

如何single从表中选择列products并将它们与类别名称一起显示在页面上?

4

1 回答 1

1

然后没有类别。避免使用您的类别类,直接从产品类中获取。创建一个函数,如:

public function singleItems()
{
   return $this->where("single_product", 1)->get();

}

然后你foreach结果出来。请检查此以获取更多信息:

https://laravel.com/docs/5.1/eloquent-collections

我知道上面是针对其他版本的 laravel 而不是你的,但我认为它应该可以工作。否则告诉我,然后我会进一步看。

于 2016-07-03T17:39:06.940 回答