将 Laravel-4 Eloquent 查询结果转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16578662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Laravel-4 Eloquent query results to arrays
提问by William Lawn Stewart
I'm trying to get my route to insert a new row into the database, and if successful return the record (with its new primary key id) in some JSON. I'm getting the following error:
我试图让我的路线在数据库中插入一个新行,如果成功返回一些 JSON 中的记录(带有新的主键 ID)。我收到以下错误:
{
"error":
{
"type":"BadMethodCallException",
"message":"Call to undefined method Illuminate\Database\Query\Builder::to_array()",
"file":"\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php",
"line":1418
}
}
This is my route:
这是我的路线:
Route::post('/client/create', function()
{
$client = Client::create(Input::all());
if($client)
{
return json_encode(array('Result' => 'OK', 'Record' => $client->to_array()));
}
else
{
return json_encode(array('Result' => 'ERROR', 'Message' => 'Error Inserting Record =('));
}
});
According to the Laravel docs I've read, you're supposed to use ->to_array()
to convert your model to an array, and ::create
returns an instance of the model if successfully inserted. I've checked the database, and the records are being inserted just fine.
根据我读过的 Laravel 文档,您应该使用->to_array()
将模型转换为数组,并::create
在成功插入后返回模型的实例。我检查了数据库,记录插入得很好。
回答by Jason Lewis
By default if you were to return an Eloquent model directly it would convert the model to it's JSON representation. This is because the __toString
magic method on each model returns the model as JSON by using the toJson
method.
默认情况下,如果您要直接返回 Eloquent 模型,它会将模型转换为它的 JSON 表示。这是因为__toString
每个模型上的魔法方法通过使用该toJson
方法将模型作为 JSON 返回。
As such the model implements both the ArrayableInterface
and JsonableInterface
. That means you can directly call toJson
or toArray
on a model.
因此,该模型同时实现了ArrayableInterface
和JsonableInterface
。这意味着您可以直接调用toJson
或toArray
在模型上。
Now you can mark columns as hidden. This means that when a model is converted into it's array or JSON representation some columns are removed (think publicly accessible API, you don't want passwords showing up!). I'm not totally sure but maybe the ID of your model is being hidden.
现在您可以将列标记为隐藏。这意味着当一个模型被转换成它的数组或 JSON 表示时,一些列会被删除(想想可公开访问的 API,你不希望密码出现!)。我不完全确定,但也许您的模型的 ID 被隐藏了。
At the end of the day, all you need to do is return the instance.
归根结底,您需要做的就是返回实例。
return $client;
It'll be converted to JSON for you automatically.
它将自动为您转换为 JSON。
回答by Dwight
The to_array()
method comes from Laravel 3. Use toArray()
in Laravel 4.
该to_array()
方法来自 Laravel 3。toArray()
在 Laravel 4 中使用。
回答by krissanawat
I see question similar you try to return data to jquery jtable and same me
我看到类似的问题,您尝试将数据返回到 jquery jtable 和我一样
Player::orderBy($sort)->paginate($pagesize)->getCollection();
return Response::view(array('Result'=>'OK','Records'=>$player->toArray()));
for try many hour I found solution getcollection pull instance from object and then solve
尝试了很多小时,我找到了解决方案 getcollection 从对象中提取实例,然后解决