php 如何从原始查询中检索结果集作为数组而不是 Laravel 3 中的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13746199/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:00:25  来源:igfitidea点击:

How to retrieve a result set from a raw query as an array instead of an object in Laravel 3

phplaravellaravel-3

提问by Ramesh

By default, Laravel's raw query methods return results as arrays of stdClass objects:

默认情况下,Laravel 的原始查询方法以 stdClass 对象数组的形式返回结果:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [username] => admin
            [password] => admin123
            [email] => [email protected]
            [created_at] => 2012-12-06 18:57:19
            [updated_at] => 2012-12-06 00:00:00
        )

    [1] => stdClass Object
        (
            [id] => 2
            [username] => userna
            [password] => user
            [email] => [email protected]
            [created_at] => 2012-12-06 00:00:00
            [updated_at] => 2012-12-05 00:00:00
        )
)

The question is how to have Laravel return an array of Arrays instead:

问题是如何让 Laravel 返回一个数组数组:

   Array
    (
        [0] => Array
            (
                [id] => 1
                [username] => admin
                [password] => admin123
                [email] => [email protected]
                [created_at] => 2012-12-06 18:57:19
                [updated_at] => 2012-12-06 00:00:00
            )

        [1] => Array
            (
                [id] => 2
                [username] => userna
                [password] => user
                [email] => [email protected]
                [created_at] => 2012-12-06 00:00:00
                [updated_at] => 2012-12-05 00:00:00
            )
    )

采纳答案by David Barker

Original answer for Laravel 3

Laravel 3 的原始答案

Eloquent has a method to_array()

Eloquent 有一个方法 to_array()

From docs:

从文档:

The to_array method will automatically grab all of the attributes on your model, as well as any loaded relationships.

to_array 方法将自动获取模型上的所有属性,以及任何加载的关系。

$user = User::find($id);

return Response::json($user->to_array());

or

或者

return Response::eloquent($user);

If you are using fluent, you can do as Sinan suggested and change the global configuration to return an associative array rather than objects.

如果您使用的是 fluent,您可以按照思南的建议进行操作,并更改全局配置以返回关联数组而不是对象。

Alternatively, you can convert an object to and from JSON to convert it to an array although the global option would be preferable in most cases. You can use the following in projects where you prefer objects normally but in certain edge cases need an array. This method will not play well with Eloquent, use the methods above in that case.

或者,您可以将对象与 JSON 相互转换以将其转换为数组,尽管在大多数情况下全局选项更可取。您可以在通常喜欢对象但在某些边缘情况下需要数组的项目中使用以下内容。这种方法不能很好地与 Eloquent 配合使用,在这种情况下使用上面的方法。

$users = DB::table('users')->where('name', '=', 'david')->get();

return array_map(function($val)
{
    return json_decode(json_encode($val), true)
}, $users);

Another option would be to temporarily change the runtime configuration

另一种选择是临时更改运行时配置

Config::set('database.fetch', PDO::FETCH_ASSOC);

For Laravel ~4

对于 Laravel ~4

In Laravel 4 onwards, all method names conform to PSR-2 standards.

从 Laravel 4 开始,所有方法名称都符合 PSR-2 标准。

$user = User::findOrFail($id);

return Response::json($user->toArray());

// In Laravel 5 onward the functions are preferred to facades.
return response()->json($user->toArray());

回答by Sinan Eldem

You may also get all the result always as array by changing

您也可以通过更改始终将所有结果作为数组

application/config/database.php

应用程序/配置/database.php

'fetch' => PDO::FETCH_CLASS,

on line 31 to

在第 31 行到

'fetch' => PDO::FETCH_ASSOC,

回答by Dale

I don't know if laravel has in built function for returning results as array but if not you can use this snippet:

我不知道 laravel 是否具有将结果作为数组返回的内置函数,但如果没有,您可以使用以下代码段:

Where $datais your returned array of objects

$data你返回的对象数组在哪里

$data = json_decode(json_encode((array) $data), true);