我正在使用Laravel 4,我需要从我的数据库中取出条目,将它们放入数组中,并在我的视图中将它们用于foreach循环中。Laravel 4 MySQL查询数组?
我没有得到任何错误,但我也没有从数据库中得到任何东西。
我试图编辑Laravel包的这个页面,以便从数据库中获取产品,而不是静态的。
这是我的模型
return $products = \DB::select('select * from products', array('sku', 'name'));
控制器我的查询......
public function getIndex()
{
// Get the products.
//
$products = Products::all();
// Show the page.
//
return View::make('shpcart::home')->with('products', $products);
}
这是我的看法
<pre>
<?php
print_r($products);
?>
</pre>
这是结果
阵列 ( )
本工程以插入到数据库:
DB::insert('insert into products (sku, name) values (?, ?)', array('WS004', 'Test'));
这...
$products = DB::table('products')->get();
foreach ($products as $product)
{
echo $product->sku;
}
返回错误
提供的foreach无效的参数()
你可以在这里看到它...
http://wilsonswan.co.uk/collection
在此先感谢。
它看起来像你没有通过$产品变量视图 –
对不起,我没想到包括控制器。我上面已经编辑过。 –
也许你应该在github上创建一个问题:https://github.com/rktaiwala/Shpcart/issues –