Laravel 5.1 DB:选择 toArray()

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

Laravel 5.1 DB:select toArray()

laravellaravel-5eloquentlaravel-5.1

提问by Yevgeniy Afanasyev

I have a large SQL statement that I am executing like so:

我有一个大型 SQL 语句,我正在执行如下:

$result = DB::select($sql);

For example

例如

$result = DB::select('select * from users');

I'd like the result to be an array - but at the moment it returns a structure like so, an array with Objects...

我希望结果是一个数组 - 但目前它返回一个像这样的结构,一个带有对象的数组......

Array
(
    0 => stdClass::__set_state(array(
        'id' => 1,
        'first_name' => 'Pavel',
        'created_at' => '2015-02-23 05:46:33',
    )),
    1 => stdClass::__set_state(array(
        'id' => 2,
        'first_name' => 'Eugene',
        'created_at' => '2016-02-23 05:46:34',
    )),
...etc...

)

)

回答by Jose Mujica

Seems like you need to cast the stdClass Objects as Array so you can have the structure you are looking for

似乎您需要将 stdClass 对象转换为数组,以便您可以拥有所需的结构

$result = array_map(function ($value) {
    return (array)$value;
}, $result);

回答by Marcin Nabia?ek

The best solutions looking at performance would be changing method how data is grabbed from database:

查看性能的最佳解决方案是更改从数据库中获取数据的方法:

// get original model
$fetchMode = DB::getFetchMode();
// set mode to custom
DB::setFetchMode(\PDO::FETCH_ASSOC);
// get data
$result = DB::select($sql);
// restore mode the original
DB::setFetchMode($fetchMode);

Obviously if you want to get all the data as array, it will be enough to change in config/database.phpfrom:

显然,如果您想将所有数据作为数组获取,那么config/database.php从以下内容更改就足够了:

'fetch'       => PDO::FETCH_CLASS,

into

进入

'fetch'       => PDO::FETCH_ASSOC,

and then running only:

然后只运行:

$result = DB::select($sql);

will return you multidimensional array instead of array of objects

将返回多维数组而不是对象数组

回答by Alexey Mezenin

toArray()method converts collection to an array. Models are converted to arrays too:

toArray()方法将集合转换为数组。模型也转换为数组:

The toArraymethod converts the collection into a plain PHP array. If the collection's values are Eloquentmodels, the models will also be converted to arrays

toArray方法将集合转换为普通的 PHP 数组。如果集合的值是Eloquent模型,模型也将被转换为数组

So, I guess you're doing something wrong. If you want more help, please update you question with all related code.

所以,我猜你做错了什么。如果您需要更多帮助,请使用所有相关代码更新您的问题。

Also, only Eloquent collections (models) have toArray()method.

此外,只有 Eloquent 集合(模型)才有toArray()方法。

To use this method, use Eloquent:

要使用此方法,请使用 Eloquent:

$result = \App\User::all()->toArray();

回答by Yevgeniy Afanasyev

Here is another approach that is worth to be mentioned:

这是另一种值得一提的方法:

$resultArray = json_decode(json_encode($result), true);

回答by Tobias Sette

For laravel/lumen 5.4 (that uses illuminate/database 5.4):

对于 laravel/lumen 5.4(使用照明/数据库 5.4):

$pdo = DB::getPdo();
$statement = $pdo->prepare($yourQuery);
$statement->setFetchMode(\PDO::FETCH_ASSOC);
$statement->execute();
$results = $statement->fetchAll();

Orlisten to Illuminate\Database\Events\StatementPrepared event: Laravel 5.4 - How to set PDO Fetch Mode?

或者听 Illuminate\Database\Events\StatementPrepared event: Laravel 5.4 - How to set PDO Fetch Mode?

回答by huuuk

Try this

尝试这个

$result = DB::select($sql);
$arr = [];
foreach($result as $row)
{
    $arr[] = (array) $row;
}

回答by Mourad MAMASSI

you can try this

你可以试试这个

DB::table('commune')->where('Commune','<>', 'ND')->orderBy('Commune')->get()->pluck('Commune');

回答by Mahmoud Mostafa

You can make a collection in order to use toArray(), very simple approach:

您可以创建一个集合以使用toArray()非常简单的方法:

$result = DB::table('users')->get();
$data = collect($result)->map(function($x){ return (array) $x; })->toArray(); 

回答by Vishal

One tricky and simple way:

一种棘手而简单的方法:

//Got array in stdClass converting to JSON $temp1= json_encode($result); //JSON to Array $temp2= json_decode($temp1,1);

//Got array in stdClass converting to JSON $temp1= json_encode($result); //JSON to Array $temp2= json_decode($temp1,1);

May be it will help you

也许它会帮助你

回答by Chandan Sharma

You can use this one. It's worked for me.

你可以用这个。它对我有用。

$query = DB::table('user')->select(['id','name'])->get();
$userlist = $query->toArray();