laravel中DB查询数据库后,返回的对象转为数组


先编码成json字符串,再解码成数组

/**
 * [objectToArray 先编码成json字符串,再解码成数组]
 * @param  [type] $object [description]
 * @return [type]         [description]
 */
public function objectToArray($object) {
    return json_decode(json_encode($object), true);
}

DB查询数据库

$res = DB::connection('mysql_crm')->table('yu_school_ersanshi')->get()->map(function ($value){
            return (array)$value;
        });

return gettype($res[0]);
//return $res[0]['id'];

原生sql查询出来的数据

$sql = "select a.zwmc,a.id,a.szgj from gwdxyxk as a left join yu_school_ersanshi as b on a.id=b.s_ys_id where a.szgj='a0920192AD18538XLtaQ' and b.content1 is null and a.zwmc is not null";
        
$res = DB::connection('mysql_crm')->select($sql);

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

return $a[0];
//return gettype($a[0]);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM