mysql - Laravel 4 MySQL 查询数组?

标签 mysql arrays laravel

我正在使用 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/

相关文章:

java - 一旦我生成了一个范围内的随机 Int,我如何将该整数分配给字母表中的每个字母?

将十进制数的每一位转换为相应的二进制数

PHP如何在这种情况下优化写入文件

python - 检查一个数组中的所有行是否都存在于另一个更大的数组中

mysql - mysqldbcompare 实用程序可以在 AIX 上使用吗?

laravel - 如何知道 Laravel 中的邮件发送或未发送以及哪个收件人没有收到邮件?

Javascript 搜索栏过滤器,在页面上隐藏/显示

php - 通过按 Enter 键提交表单后 Laravel 错误

mysql - 不确定如何进行正确的 mySQL 查询以在 1 个查询中从多个表获取数据

java - 为什么我的代码没有抛出 EntityExistsException