显示Laravel类别及最低价格

时间:2016-06-08 17:33:27

标签: php mysql laravel laravel-4

尝试显示数据库中的所有类别,并根据类别中的产品显示最低价格。所以到目前为止我所拥有的是: 模型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
    ]); 
}

1 个答案:

答案 0 :(得分:3)

在您看来,您可能有一个#ifndef CTEST_H #define CTEST_H class CTest { int value; public: CTest(int); int getValue(); struct CTESTSTRUCT { union CTESTUNION { enum CTestEnum { var1 = 1 }; char varChar1 = 'Y'; char varChar2 /*= 'N'*/; //union atmost have one field initializer }; int structValue(); int testVal; }; ~CTest(); }; #endif //************************************************************************************ #include <iostream> #include "CTest.h" CTest::CTest(int argVal) : value(argVal) { std::cout << "Constructor Called" << std::endl; } int CTest::getValue() { std::cout << "getValue Called" << std::endl; return value; } int CTest::CTESTSTRUCT::structValue() { std::cout << "CTESTSTRUCT::setValue Called" << std::endl; return CTESTSTRUCT::CTESTUNION::var1; } CTest::~CTest() { std::cout << "Distructor Called" << std::endl; } //********************************************************************************* #include <iostream> #include "CTest.h" using namespace std; CTest * ctestObj; int main() { ctestObj = new CTest(25); int returnVal = ctestObj->getValue(); std::cout << "Value Returned: " << returnVal << std::endl; std::cout << "structVal: " << std::endl; //CTest::CTESTSTRUCT::testVal = 10; delete ctestObj; return 0; } 声明。把它放在那里:

foreach

这是该类别中产品的最低价格。