在laravel中,使用DB查询数据库后,如果在 get() 后直接使用 toArray() 返回的数据是 php 标准的 stdClass

$goods = DB::connection('system')
        ->table('group_buy_goods')
        ->get(['id', 'name', 'sold_price']);
dd($goods->toArray());

2022-04-19T03:51:27.png

如果想把对象改为数组可以使用 map 方法修改数据格式

$goods = Db::connection('system')
        ->table('group_buy_goods')
        ->get(['id', 'name', 'sold_price'])
        ->map(function ($value) {
            return (array)$value;
        });
dd($goods->toArray());

2022-04-19T03:54:35.png

使用 Eloquent 模型查询的数据是数组
GroupBuyGoods::query()->get()->toArray()

感兴趣的可以试下

标签: php, laravel