$res=DB::select('select * from qq');
var_dump($res);
$res=DB::table('qq')->get();
var_dump($res);
Why is a one-dimensional array obtained in the picture after querying in these two ways? Why are there objects in one-dimensional arrays? What we get using native query is a two-dimensional array. Why is the result not a two-dimensional array?
The following is Laravel 5.4 version, because I have not used Laravel 5.2 version.
Run the native SQL query and get a result set in the form of an array. See the documentation for details.
$res = DB::select('select * from qq');
dd($res);
/*
* array:5 [▼
0 => {#388 ?}
1 => {#399 ?}
2 => {#400 ?}
3 => {#401 ?}
4 => {#402 ?}
]
*/
Use the Query Builder to run the SQL statement and get a Collection object. See the documentation for details.
$res = DB::table('qq')->get();
dd($res);
/*
* Collection {#398 ▼
#items: array:5 [▼
0 => {#399 ?}
1 => {#400 ?}
2 => {#401 ?}
3 => {#402 ?}
4 => {#403 ?}
]
}
*/
What you get when you run a native SQL statement is a one-dimensional array wrapped in objects, not a two-dimensional array.
What you get using the query constructor is a Collection object, which is also a one-dimensional array wrapping each object. So why return the Collection object, because it has many useful and elegant methods built in. Just like Eloquent returns Collection objects by default.
//返回?cái)?shù)組 laravel 默認(rèn)返回的為對(duì)象
$res=DB::table('qq')->get()->toarray;
dump($res);