Laravel版本:laravel-laravel-936160f
$data = entities\org::all();
return var_dump($data);
我在all()调用上获取数据,但我只想发出列元组。而且,因为我无法在$ data上调用toJson(),所以我将结果输出为var_dump:
array(612){
[0] => object(entities \ Org)#34(5){
[ “属性”] => array(3){
[ “org_id”] => int(1)
[ “名称”] => string(28)“some name”
[ “SHORT_NAME”] => string(15)“some short name”
} [“original”] => array(3){
[ “org_id”] => int(1)
[ “名称”] => string(28)“some name”
[ “SHORT_NAME”] => string(15)“some short name”
} [“relationships”] => array(0){}
[ “存在”] =>布尔(真)
[ “包括”] => array(0){}
}
[1] => ...
如何获得基本的json元组? (org_id,名称,短名称)。我可以迭代数据数组,但我认为必须有更快/更好的方法。
答案 0 :(得分:3)
在Laravel 3中:
$data = entities\org::all();
return Response::eloquent($data);
在Laravel 4中:
return entities\org::all();
Laravel 3和Laravel 4都提供了返回json表示的自动方法。
Laravel 3资源:
http://laravel.com/docs/database/eloquent#to-array https://github.com/laravel/laravel/blob/master/laravel/helpers.php#L281 https://github.com/laravel/laravel/blob/master/laravel/response.php#L136 https://github.com/laravel/laravel/blob/master/laravel/database/eloquent/model.php#L589
Laravel 4资源:
http://four.laravel.com/docs/eloquent#converting-to-arrays-or-json https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L1409 https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Collection.php#L200
希望这一切都有所帮助。