DB query builder toArray() laravel 4

前端 未结 7 1241
野性不改
野性不改 2021-02-01 13:32

I\'m trying to convert a query to an array with the method toArray() but it doesn\'t work for the query builder. Any ideas for convert it?

Example



        
7条回答
  •  有刺的猬
    2021-02-01 13:51

    And another solution

    $objectData = DB::table('user')
        ->select('column1', 'column2')
        ->where('name', '=', 'Jhon')
        ->get();
    $arrayData = array_map(function($item) {
        return (array)$item; 
    }, $objectData->toArray());
    

    It good in case when you need only several columns from entity.

提交回复
热议问题