Db::table('user')->where('id', $id)->first()->toArray();
這樣使用會報錯,Call to undefined method stdClass::toArray() 但是我看IDE提示,有這個方法
你是用的 DB::table()->first() 查詢,返回的結(jié)果是 stdClass。stdClass 不支持 toArray 操作。
請問用什么方式查詢,可以獲取數(shù)組,或者轉(zhuǎn)成數(shù)組
(array) DB::table()->first() 這樣會直接轉(zhuǎn)數(shù)組。stdClass 支持這種轉(zhuǎn)換。
你可以考慮使用 laravel 的 orm model 進行查詢,查詢結(jié)果支持調(diào)用 ->toArray()
用QueryBuilder才有toArray()
->query() 對吧
DB返回就是數(shù)組 模型返回的是對象可以toArray()
直接轉(zhuǎn)數(shù)組 : $arr = (array)Db::table("xxxx")->where("id",1)->first();