如何从原始查询作为数组而不是Laravel 3中的对象检索结果集 [英] How to retrieve a result set from a raw query as an array instead of an object in Laravel 3

查看:91
本文介绍了如何从原始查询作为数组而不是Laravel 3中的对象检索结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [username] => admin
            [password] => admin123
            [email] => admin@admin.com
            [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] => user@gmail.com
            [created_at] => 2012-12-06 00:00:00
            [updated_at] => 2012-12-05 00:00:00
        )
)

问题是如何让Laravel代替返回数组数组:

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

   Array
    (
        [0] => Array
            (
                [id] => 1
                [username] => admin
                [password] => admin123
                [email] => admin@admin.com
                [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] => user@gmail.com
                [created_at] => 2012-12-06 00:00:00
                [updated_at] => 2012-12-05 00:00:00
            )
    )

推荐答案

Laravel 3的原始答案

口才有方法to_array()

来自文档:

to_array方法将自动获取所有属性 您的模型以及所有已加载的关系.

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

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

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

return Response::eloquent($user);

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

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

或者,您可以将对象与JSON相互转换,以将其转换为数组,尽管在大多数情况下,全局选项是更可取的.您可以在通常需要对象但在某些情况下需要数组的项目中使用以下内容.这种方法不适用于Eloquent,请在这种情况下使用上面的方法.

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.

$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);

对于Laravel〜4

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

For Laravel ~4

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

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

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

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

这篇关于如何从原始查询作为数组而不是Laravel 3中的对象检索结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆