【问题标题】:Laravel, bringing an array to viewLaravel,带来一个数组来查看
【发布时间】:2020-05-29 11:58:32
【问题描述】:
        $vegetables = DB::select("select a.id, a.created_at, a.updated_at,a.storeid, a.itemno, a.type, a.vegie from stores a, storenames b where a.storeid = b.storeid");
        return view('stores', ['vegetables' => $vegetables]);

我使用上面的代码查询数据库,在查看页面出现如下错误:

 @foreach($vegetables as $vegetable)
        <tbody>
            <tr>
                <th scope="row">{{ $vegetable['storeid'] }}</th>
                <td>{{ $vegetable['itemno'] }}</td>
                <td>{{ $vegetable['type'] }}</td>
                <td>{{ $vegetable['vegie'] }}</td>
            </tr>
            @endforeach
        </tbody>

Facade\Ignition\Exceptions\ViewException 不能将 stdClass 类型的对象用作数组

任何帮助将不胜感激。

【问题讨论】:

  • 请在查询中添加表名
  • 我认为你缺少表名
  • 你错了,他用的是表:from stores a
  • @SergheiLeonenco,之前尝试过,但它不起作用
  • 你的视图是什么样的?

标签: php laravel


【解决方案1】:

似乎您需要将 stdClass 对象转换为数组,以便您可以拥有您正在寻找的结构

$vegetables = array_map(function ($value) {
    return (array)$value;
}, $vegetables);

【讨论】:

  • 调用数组上的成员函数 toArray()
  • 你试过没有toArray() 像一个收藏吗?
猜你喜欢
  • 2021-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-07
相关资源
最近更新 更多