2017-09-18 3 views
0

어떻게이 Laravel 컬렉션 배열을 아래와 같이 json 형식으로 변환 할 수 있습니까?Laravel 컬렉션 배열을 json으로 변환하는 방법

//All records from users table. 
$users = DB::table('users')->get(); 

// Required json format. 
return '{ 
      "data": [ 

      { 
       "DT_RowId": "row_1", 
       "id": "Tiger", 
       "clear": "Nixon", 
       "fsssf": "System Architect", 
       "tex": "[email protected]", 
       "created_at": "Edinburgh", 
       "updated_at": "Edinburgh" 
      }, 
      { 
       "DT_RowId": "row_2", 
       "id": "Tiger", 
       "clear": "Nixon", 
       "fsssf": "System Architect", 
       "tex": "[email protected]", 
       "created_at": "Edinburgh", 
       "updated_at": "Edinburgh" 
      } 

      ], 
    "options": [], 
    "files": [] 
}'; 

미안하지만이 jso로 변환 할 수 없습니다.

+0

$ users는 컬렉션이며 "-> toJson()"메소드가 있습니다. – inet123

답변

1

documentation을 살펴보십시오.

toJson()을 사용하여 컬렉션을 json 개체로 변환 할 수 있습니다.

$users = DB::table('users')->get()->toJson(); 
dd($users); 

또한

$users = json_decode($users); 

link

인사 및 코딩 행복에서보세요 사용하여 간단한 PHP 방법으로이 작업을 수행 할 수 있습니다!

0

커플을 사용해야합니다 5.5 laravel 경우에는 기본 방법을 사용할 수 있습니다 ->이

$users = DB::table('users')->get(); 
$users-toJson(); 

등 사용자 컬렉션 toJson()를하는 경우 그들을 여기로 json_encode 방법에 구축 PHP로 할 수있는 그것으로 문제가 있습니까하면 전체 문서를이 도움이 http://php.net/manual/en/function.json-encode.php

$users = DB::table('users')->get(); 
json_encode($users) 

희망입니다 !

0
return Response::json([ 
    'data' => $value 
], 200); 

희망이 도움이 되셨습니다.

관련 문제