Why is laravel5.2 query result not a two-dimensional array?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-06-06 09:54:43
0
2
808
$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?

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(2)
阿神

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.

淡淡烟草味
//返回数组 laravel 默认返回的为对象
$res=DB::table('qq')->get()->toarray;
dump($res);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template