我正在使用 Laravel 4,我需要从我的数据库中获取条目,将它们放入一个数组中并在我认为的 foreach 循环中使用它们。
我没有收到任何错误,但我也没有从数据库中获取任何信息。
我正在尝试编辑 Laravel 包的这个页面以从数据库中获取产品,而不是静态地获取产品。
这是我的模型查询
return $products = \DB::select('select * from products', array('sku', 'name'));
Controller ...
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
提前致谢。
最佳答案
您不应该这样退回 $products。
你也许应该做这样的事情:
public function index()
{
$products = Products::where('sku', '=', 'name')->get();
return View::make('view', compact('products'));
}
关于mysql - Laravel 4 MySQL 查询数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19094909/