在laravel中,使用DB查询数据库后,返回的对象,可以用下面的办法变为数组
在laravel中,使用DB查询数据库后,如果在 get()
后直接使用 toArray()
返回的数据是 php
标准的 stdClass
$goods = DB::connection('system')
->table('group_buy_goods')
->get(['id', 'name', 'sold_price']);
dd($goods->toArray());
如果想把对象改为数组可以使用 map
方法修改数据格式
$goods = Db::connection('system')
->table('group_buy_goods')
->get(['id', 'name', 'sold_price'])
->map(function ($value) {
return (array)$value;
});
dd($goods->toArray());
使用 Eloquent 模型查询的数据是数组
GroupBuyGoods::query()->get()->toArray()
感兴趣的可以试下
评论已关闭