从Laravel 4的2张桌子中选择

时间:2014-07-30 08:30:08

标签: php laravel-4 eloquent

我的选择有问题,所以我有两张桌子:

category
id    name
product
id  name  category_id

现在我想选择一个类别的产品:

public function getCategory($cat_id)
{
    return View::make('store.category')
        ->with('products', Product::where('category_id', '=' , $cat_id))
}

此查询为空,存在另一种解决方案 - 请帮帮我。

1 个答案:

答案 0 :(得分:0)

你的功能不是很正确。您没有在查询上调用get(),因此它实际上并未运行。而且,你没有正确地传递产品。试试这个

public function getCategory($cat_id)
{
    $products = Product::where('category_id', '=' , $cat_id)->get();

    return View::make('store.category')
        ->with(array('products' => $products));
}