laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.7k stars 11.06k forks source link

[5.8] toArray with Query Builder #27722

Closed jimgwhit closed 5 years ago

jimgwhit commented 5 years ago

In eloquent if I do the following (just example here):

$dogs = Dog::where('adopted', '=', 0)->get()->toArray();

No problem it works,

Output:

array(14) {
  [0]=>
  array(7) {
    ["dogid"]=>
    int(91)
    ["dogpic"]=>
    string(12) "dallas77.jpg"
    ["dogname"]=>
    string(7) "Dalla's"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(227) "2 year lab shepard mix."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2016-09-18 17:44:31"
  }
  [1]=>
  array(7) {
    ["dogid"]=>
    int(192)
    ["dogpic"]=>
    string(12) "danny173.jpg"
    ["dogname"]=>
    string(5) "Danny"
    // more

However after trying both of these using query builder:

$dogs = DB::table('dc_dogs')->where('adopted', '=', 0)->get()->toArray();

// and

$dogs = DB::table('dc_dogs')->where('adopted', '=', 0)->get();
$dogarray = (array)$dogs;

Both only return:

object(Illuminate\Support\Collection)#247 (1) {
  ["items":protected]=>
  array(14) {
    [0]=>
    object(stdClass)#249 (7) {
      ["dogid"]=>
      int(91)
      ["dogpic"]=>
      string(12) "dallas77.jpg"
      ["dogname"]=>
      string(7) "Dalla's"
      ["sex"]=>
      string(1) "M"
      // more

Shouldn't there be a way to easily have a ->toArray() in query builder. In fact the db facade is the same.

If that's by design, could you at least answer why okay for eloquent but not query builder. Thanks.

DariusIII commented 5 years ago

Have you tried:

Dog::query()->where('adopted', '=', 0)->get()->toArray();

stancl commented 5 years ago
>>> DB::table('products')->select('id')->get()->toArray();
=> [
     {#3255
       +"id": 1,
     },
     {#3253
       +"id": 2,
     },
     {#3243
       +"id": 5,
     },
   ]

get() returns an instance of Illuminate\Support\Collection from both the query builder and Eloquent so the behavior should be the same (toArray() is a method on Collection).

jimgwhit commented 5 years ago

QUOTE

DB::table('products')->select('id')->get()->toArray();

UNQUOTE

Really only one field and no where clause, not even the same. How about show me with a where clause (NOT JUST ONE FIELD) that you can get it working.

and

Dog::query()->where('adopted', '=', 0)->get()->toArray();

That's orm, it works with orm already.

stancl commented 5 years ago

After writing that comment I tried it without the select and with a where and it still worked.

jimgwhit commented 5 years ago

After writing that comment I tried it without the select and with a where and it still worked.

Can you show code. And results.

stancl commented 5 years ago

Sure.

Psy Shell v0.9.9 (PHP 7.2.15-0ubuntu0.18.04.1 — cli) by Justin Hileman
>>> DB::table('products')->where('name', 'Product A')->get()->toArray();
=> [
     {#3196
       +"id": 1,
       +"name": "Product A",
       +"price": 50.73,
       +"tax": 15.0,
       +"stock": "46",
       +"group_pack": 999999999,
       +"min_amount": 1,
       +"enabled": 1,
       +"description": null,
       +"created_at": "2018-07-14 22:00:00",
       +"updated_at": "2018-12-27 20:34:03",
     },
   ]
>>> 
jimgwhit commented 5 years ago

Still get object(stdClass)

$dogs = DB::table('dc_dogs')->select('dogid', 'dogname')->where('adopted', '=', 0)->get()->toArray();
array(14) {
  [0]=>
  object(stdClass)#250 (2) {
    ["dogid"]=>
    int(91)
    ["dogname"]=>
    string(7) "Dalla's"
  }
  [1]=>
  object(stdClass)#252 (2) {
    ["dogid"]=>
    int(192)
    ["dogname"]=>
    string(5) "Danny"
  }
  [2]=>
  object(stdClass)#253 (2) {
    ["dogid"]=>
    int(310)
    ["dogname"]=>
    string(7) "Lil Bit"
  }
  [3]=>
  object(stdClass)#255 (2) {
    ["dogid"]=>
    int(328)
    ["dogname"]=>
    string(5) "Ruffy"
  }
  [4]=>
  object(stdClass)#256 (2) {
    ["dogid"]=>
    int(340)
    ["dogname"]=>
    string(2) "CJ"
  }
  [5]=>
  object(stdClass)#257 (2) {
    ["dogid"]=>
    int(364)
    ["dogname"]=>
    string(7) "Roxanne"
  }
  [6]=>
  object(stdClass)#258 (2) {
    ["dogid"]=>
    int(368)
    ["dogname"]=>
    string(5) "Greta"
  }
  [7]=>
  object(stdClass)#259 (2) {
    ["dogid"]=>
    int(372)
    ["dogname"]=>
    string(4) "Bart"
  }
  [8]=>
  object(stdClass)#260 (2) {
    ["dogid"]=>
    int(375)
    ["dogname"]=>
    string(3) "Boy"
  }
  [9]=>
  object(stdClass)#261 (2) {
    ["dogid"]=>
    int(378)
    ["dogname"]=>
    string(4) "Blue"
  }
  [10]=>
  object(stdClass)#262 (2) {
    ["dogid"]=>
    int(379)
    ["dogname"]=>
    string(4) "Pink"
  }
  [11]=>
  object(stdClass)#263 (2) {
    ["dogid"]=>
    int(380)
    ["dogname"]=>
    string(3) "Red"
  }
  [12]=>
  object(stdClass)#264 (2) {
    ["dogid"]=>
    int(381)
    ["dogname"]=>
    string(3) "Zoe"
  }
  [13]=>
  object(stdClass)#265 (2) {
    ["dogid"]=>
    int(383)
    ["dogname"]=>
    string(6) "Stormy"
  }
}
stancl commented 5 years ago

Run composer update, your query again and if the problem persists send the output of php artisan --version here.

stancl commented 5 years ago

Actually. Your output ... is an array? What is the problem? You're running toArray() and you're getting an array. An array of objects.

jimgwhit commented 5 years ago

@stancl try it and do more than just one item.

{#3196
       +"id": 1,
       +"name": "Product A",
       +"price": 50.73,
       +"tax": 15.0,
       +"stock": "46",
       +"group_pack": 999999999,
       +"min_amount": 1,
       +"enabled": 1,
       +"description": null,
       +"created_at": "2018-07-14 22:00:00",
       +"updated_at": "2018-12-27 20:34:03",
     },

is an object

stancl commented 5 years ago

Look at my comment above. I'm getting an array of objects just like you are. That's the intended behavior.

stancl commented 5 years ago
>>> DB::table('products')->whereIn('name', ['Product A', 'Product B'])->get()->toArray();
=> [
     {#3226
       +"id": 1,
       +"name": "Product A",
       +"price": 50.73,
       +"tax": 15.0,
       +"stock": "46",
       +"group_pack": 999999999,
       +"min_amount": 1,
       +"enabled": 1,
       +"description": null,
       +"created_at": "2018-07-14 22:00:00",
       +"updated_at": "2018-12-27 20:34:03",
     },
     {#3228
       +"id": 2,
       +"name": "Product B",
       +"price": 165.0,
       +"tax": 21.0,
       +"stock": "0",
       +"group_pack": 55,
       +"min_amount": 1,
       +"enabled": 1,
       +"description": null,
       +"created_at": "2018-07-14 22:00:00",
       +"updated_at": "2018-12-17 18:36:05",
     },
   ]

If you want to turn the objects into associative arrays, you can do this for example:

>>> json_decode(DB::table('products')->whereIn('name', ['Product A', 'Product B'])->get()->toJson(), true);
=> [
     [
       "id" => 1,
       "name" => "Product A",
       "price" => 50.73,
       "tax" => 15,
       "stock" => "46",
       "group_pack" => 999999999,
       "min_amount" => 1,
       "enabled" => 1,
       "description" => null,
       "created_at" => "2018-07-14 22:00:00",
       "updated_at" => "2018-12-27 20:34:03",
     ],
     [
       "id" => 2,
       "name" => "Product B",
       "price" => 165,
       "tax" => 21,
       "stock" => "0",
       "group_pack" => 55,
       "min_amount" => 1,
       "enabled" => 1,
       "description" => null,
       "created_at" => "2018-07-14 22:00:00",
       "updated_at" => "2018-12-17 18:36:05",
     ],
   ]
jimgwhit commented 5 years ago

Look at my original post above where I use

$dogs = Dog::where('adopted', '=', 0)->get()->toArray();

It returns array's in array's, the orm works is my whole point, but the query builder returns object(stdClass). That is the whole issue, ->toArray(); isn't working in query builder the same.

PLEASE see the first post and see the difference.

stancl commented 5 years ago

Send the outputs of

Dog::where('id', 1)->get();

And

DB::table('dogs')->where('id', 1)->get();

jimgwhit commented 5 years ago

ORM RETURNS:

array(14) {
  [0]=>
  array(7) {
    ["dogid"]=>
    int(91)
    ["dogpic"]=>
    string(12) "dallas77.jpg"
    ["dogname"]=>
    string(7) "Dalla's"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(227) "2 year lab shepard mix. Gets along well with some dogs, but not dominate dogs. Protective of his area, but once he knows you he loves play time. He has played fine with some puppies we've had. Better suited for only dog family."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2016-09-18 17:44:31"
  }
  [1]=>
  array(7) {
    ["dogid"]=>
    int(192)
    ["dogpic"]=>
    string(12) "danny173.jpg"
    ["dogname"]=>
    string(5) "Danny"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(193) "1 year, I was here as a puppy and I was adopted.  My new family couldn't take care of me, please be my new forever family. It takes me a while to get used of other pets, so please work with me."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2016-03-19 17:29:00"
  }
  [2]=>
  array(7) {
    ["dogid"]=>
    int(310)
    ["dogpic"]=>
    string(13) "lilbit280.jpg"
    ["dogname"]=>
    string(7) "Lil Bit"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(127) "About 1 1/2 on 3-1-17, He is very nervous around someone he's not used to. But he's a very loving dog once he gets to know you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-04-05 18:52:50"
  }
  [3]=>
  array(7) {
    ["dogid"]=>
    int(328)
    ["dogpic"]=>
    string(12) "ruffy296.jpg"
    ["dogname"]=>
    string(5) "Ruffy"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(110) "About 2 1/2 months on 4-10-17, was found wandering, no one has claimed me in 2 weeks, I need a good safe home."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-04-11 16:20:29"
  }
  [4]=>
  array(7) {
    ["dogid"]=>
    int(340)
    ["dogpic"]=>
    string(9) "cj303.jpg"
    ["dogname"]=>
    string(2) "CJ"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(97) "About one year on 5-10-17, I am friendly and need a new home. Owner moved and I didn't get to go."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-05-10 18:56:50"
  }
  [5]=>
  array(7) {
    ["dogid"]=>
    int(364)
    ["dogpic"]=>
    string(14) "roxanne325.jpg"
    ["dogname"]=>
    string(7) "Roxanne"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(79) "ShihTzu- 2 years as of 8-9-17, timid at first, but friendly once she knows you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-09 11:57:11"
  }
  [6]=>
  array(7) {
    ["dogid"]=>
    int(368)
    ["dogpic"]=>
    string(12) "greta328.jpg"
    ["dogname"]=>
    string(5) "Greta"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(66) "Papillon Mix, 2 years on 8-9-17. Very friendly once she knows you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-09 12:42:39"
  }
  [7]=>
  array(7) {
    ["dogid"]=>
    int(372)
    ["dogpic"]=>
    string(11) "bart331.jpg"
    ["dogname"]=>
    string(4) "Bart"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(82) "About 4 months on 8-18-17, A little nervous in new surroundings but very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-27 21:35:02"
  }
  [8]=>
  array(7) {
    ["dogid"]=>
    int(375)
    ["dogpic"]=>
    string(10) "boy334.jpg"
    ["dogname"]=>
    string(3) "Boy"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(91) "About 1 yr on 8-21-17, my owner moved and I couldn't go. Friendly once he gets to know you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-21 14:23:35"
  }
  [9]=>
  array(7) {
    ["dogid"]=>
    int(378)
    ["dogpic"]=>
    string(11) "blue337.jpg"
    ["dogname"]=>
    string(4) "Blue"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(71) "About 1 yr on 8-21-17, my owner moved and I couldn't go. Very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-21 17:32:22"
  }
  [10]=>
  array(7) {
    ["dogid"]=>
    int(379)
    ["dogpic"]=>
    string(11) "pink338.jpg"
    ["dogname"]=>
    string(4) "Pink"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(71) "About 1 yr on 8-21-17, my owner moved and I couldn't go. Very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-21 17:32:57"
  }
  [11]=>
  array(7) {
    ["dogid"]=>
    int(380)
    ["dogpic"]=>
    string(10) "red339.jpg"
    ["dogname"]=>
    string(3) "Red"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(71) "About 2 yr on 8-21-17, my owner moved and I couldn't go. Very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2019-02-07 17:09:36"
  }
  [12]=>
  array(7) {
    ["dogid"]=>
    int(381)
    ["dogpic"]=>
    string(10) "zoe340.jpg"
    ["dogname"]=>
    string(3) "Zoe"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(66) "6 months on 8-21-17, I am good and friendly in need of a new home."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2019-01-19 18:08:13"
  }
  [13]=>
  array(7) {
    ["dogid"]=>
    int(383)
    ["dogpic"]=>
    string(13) "stormy342.jpg"
    ["dogname"]=>
    string(6) "Stormy"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(98) "One year on 8-25-17, I was found and brought here, I almost got hit by a car. Will be held 3 days."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2018-05-06 16:52:37"
  }
}

Query builder:

array(14) {
  [0]=>
  object(stdClass)#251 (7) {
    ["dogid"]=>
    int(91)
    ["dogpic"]=>
    string(12) "dallas77.jpg"
    ["dogname"]=>
    string(7) "Dalla's"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(227) "2 year lab shepard mix. Gets along well with some dogs, but not dominate dogs. Protective of his area, but once he knows you he loves play time. He has played fine with some puppies we've had. Better suited for only dog family."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2016-09-18 17:44:31"
  }
  [1]=>
  object(stdClass)#250 (7) {
    ["dogid"]=>
    int(192)
    ["dogpic"]=>
    string(12) "danny173.jpg"
    ["dogname"]=>
    string(5) "Danny"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(193) "1 year, I was here as a puppy and I was adopted.  My new family couldn't take care of me, please be my new forever family. It takes me a while to get used of other pets, so please work with me."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2016-03-19 17:29:00"
  }
  [2]=>
  object(stdClass)#253 (7) {
    ["dogid"]=>
    int(310)
    ["dogpic"]=>
    string(13) "lilbit280.jpg"
    ["dogname"]=>
    string(7) "Lil Bit"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(127) "About 1 1/2 on 3-1-17, He is very nervous around someone he's not used to. But he's a very loving dog once he gets to know you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-04-05 18:52:50"
  }
  [3]=>
  object(stdClass)#255 (7) {
    ["dogid"]=>
    int(328)
    ["dogpic"]=>
    string(12) "ruffy296.jpg"
    ["dogname"]=>
    string(5) "Ruffy"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(110) "About 2 1/2 months on 4-10-17, was found wandering, no one has claimed me in 2 weeks, I need a good safe home."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-04-11 16:20:29"
  }
  [4]=>
  object(stdClass)#256 (7) {
    ["dogid"]=>
    int(340)
    ["dogpic"]=>
    string(9) "cj303.jpg"
    ["dogname"]=>
    string(2) "CJ"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(97) "About one year on 5-10-17, I am friendly and need a new home. Owner moved and I didn't get to go."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-05-10 18:56:50"
  }
  [5]=>
  object(stdClass)#257 (7) {
    ["dogid"]=>
    int(364)
    ["dogpic"]=>
    string(14) "roxanne325.jpg"
    ["dogname"]=>
    string(7) "Roxanne"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(79) "ShihTzu- 2 years as of 8-9-17, timid at first, but friendly once she knows you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-09 11:57:11"
  }
  [6]=>
  object(stdClass)#258 (7) {
    ["dogid"]=>
    int(368)
    ["dogpic"]=>
    string(12) "greta328.jpg"
    ["dogname"]=>
    string(5) "Greta"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(66) "Papillon Mix, 2 years on 8-9-17. Very friendly once she knows you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-09 12:42:39"
  }
  [7]=>
  object(stdClass)#259 (7) {
    ["dogid"]=>
    int(372)
    ["dogpic"]=>
    string(11) "bart331.jpg"
    ["dogname"]=>
    string(4) "Bart"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(82) "About 4 months on 8-18-17, A little nervous in new surroundings but very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-27 21:35:02"
  }
  [8]=>
  object(stdClass)#260 (7) {
    ["dogid"]=>
    int(375)
    ["dogpic"]=>
    string(10) "boy334.jpg"
    ["dogname"]=>
    string(3) "Boy"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(91) "About 1 yr on 8-21-17, my owner moved and I couldn't go. Friendly once he gets to know you."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-21 14:23:35"
  }
  [9]=>
  object(stdClass)#261 (7) {
    ["dogid"]=>
    int(378)
    ["dogpic"]=>
    string(11) "blue337.jpg"
    ["dogname"]=>
    string(4) "Blue"
    ["sex"]=>
    string(1) "M"
    ["comments"]=>
    string(71) "About 1 yr on 8-21-17, my owner moved and I couldn't go. Very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-21 17:32:22"
  }
  [10]=>
  object(stdClass)#262 (7) {
    ["dogid"]=>
    int(379)
    ["dogpic"]=>
    string(11) "pink338.jpg"
    ["dogname"]=>
    string(4) "Pink"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(71) "About 1 yr on 8-21-17, my owner moved and I couldn't go. Very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2017-08-21 17:32:57"
  }
  [11]=>
  object(stdClass)#263 (7) {
    ["dogid"]=>
    int(380)
    ["dogpic"]=>
    string(10) "red339.jpg"
    ["dogname"]=>
    string(3) "Red"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(71) "About 2 yr on 8-21-17, my owner moved and I couldn't go. Very friendly."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2019-02-07 17:09:36"
  }
  [12]=>
  object(stdClass)#264 (7) {
    ["dogid"]=>
    int(381)
    ["dogpic"]=>
    string(10) "zoe340.jpg"
    ["dogname"]=>
    string(3) "Zoe"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(66) "6 months on 8-21-17, I am good and friendly in need of a new home."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2019-01-19 18:08:13"
  }
  [13]=>
  object(stdClass)#265 (7) {
    ["dogid"]=>
    int(383)
    ["dogpic"]=>
    string(13) "stormy342.jpg"
    ["dogname"]=>
    string(6) "Stormy"
    ["sex"]=>
    string(1) "F"
    ["comments"]=>
    string(98) "One year on 8-25-17, I was found and brought here, I almost got hit by a car. Will be held 3 days."
    ["adopted"]=>
    int(0)
    ["lastedit"]=>
    string(19) "2018-05-06 16:52:37"
  }
}

SEE the difference.

Stop using dd, use pre and print_r, shows results better.

stancl commented 5 years ago
  1. I'm not using dd(), I'm using artisan tinker
  2. Can you run the queries I asked you to run?
jimgwhit commented 5 years ago

@[stancl] really, toarray does not work the same with orm and querybuilder, look at outputs above please.

stancl commented 5 years ago

Can you run the queries I asked you to run?

stancl commented 5 years ago

Seems like llluminate\Database\Eloquent\Collection (returned by Eloquent) converts the inner objects into associative arrays while Illuminate\Support\Collection (returned by the query builder) converts only the outer collection into an array.

The query builder shouldn't return an Eloquent Collection, but the base Collection shouldn't convert the inner objects into arrays.The toArray() method is identical for both classes. The issue is that the query builder contains non-Arrayable stdObjects.

stancl commented 5 years ago

The docs state that

toArray also converts all of the collection's nested objects to an array. If you want to get the raw underlying array, use the all method instead.

This is not the case for the Support Collection.

Are the docs wrong or is this a bug?

>>> DB::table('products')->where('id', 1)->get()
=> Illuminate\Support\Collection {#3257
     all: [
       {#3260
         +"id": 1,
         +"name": "Product A",
         +"price": 50.73,
         +"tax": 15.0,
         +"stock": "46",
         +"group_pack": 999999999,
         +"min_amount": 1,
         +"enabled": 1,
         +"description": null,
         +"created_at": "2018-07-14 22:00:00",
         +"updated_at": "2018-12-27 20:34:03",
       },
     ],
   }
>>> DB::table('products')->where('id', 1)->get()->toArray();
=> [
     {#3263
       +"id": 1,
       +"name": "Product A",
       +"price": 50.73,
       +"tax": 15.0,
       +"stock": "46",
       +"group_pack": 999999999,
       +"min_amount": 1,
       +"enabled": 1,
       +"description": null,
       +"created_at": "2018-07-14 22:00:00",
       +"updated_at": "2018-12-27 20:34:03",
     },
   ]

The object should be an associative array per the docs.

The issue is that the returned stdObjects are not instances of Arrayable

https://github.com/laravel/framework/blob/bd867702793d489950b5fd9bfdb944a94ca6ab62/src/Illuminate/Support/Collection.php#L1824-L1829

Fixing this is a breaking change so the ideal approach would be fixing this in 5.9 and specifying that toArray() doesn't convert stdObjects to associative arrays.

jimgwhit commented 5 years ago

I thought ORM and query builder returned same type of results: As a reminder from original at top, this works "out of box" with ORM

$dogs = Dog::where('adopted', '=', 0)->get()->toArray();

Perfect, no problems, array's in an array.

HOWEVER

$dogs = DB::table('dc_dogs')->where('adopted', '=', 0)->get()->toArray();

NO GO.

Yes I can do hacks:

$dogs = DB::table('dc_dogs')->where('adopted', '=', 0)->get();
$data = collect($dogs)->map(function($x){ return (array)$x; })->toArray();

But if orm and querybuilder are returning same type results, and orm ->toArray() works but query builder ->toArray() does not work is the problem.

So is the orm and query builder returning the same type of results, or are they different type of results.

I never noticed prior, but was trying to help someone on the forum and suggested ->toArray().

My example to them was using orm, they were using query builder. That's how I stumbled on this.

mfn commented 5 years ago

Hello there,

what you are seeing is expected and not a bug (at least: not per se).

Eloquent models are powerful, rich and full of magic. One of their magic is that they are Arrayable.

Why is this relevant?

When you call Support\Collection::toArray() it iterates over all the items and

For all it's magic, in this case it actually does no magic.

What you describe as

Yes I can do hack

Is in fact not the hack but the way to do it.

Now: if you think this behaviour is wrong and needs to be changed, please open an issue at https://github.com/laravel/ideas and invite people to the discussion.

stancl commented 5 years ago

The docs are kind of misleading though

toArray also converts all of the collection's nested objects to an array. If you want to get the raw underlying array, use the all method instead.

It should be mentioned that it doesn't convert all objects into arrays.

mfn commented 5 years ago

Please feel free to improve the docs at https://github.com/laravel/docs

jimgwhit commented 5 years ago

@mfn well that answered the issue, like I said I only noticed it because I was helping someone on the forum.

I would say, it would be nice if orm, query builder and db facade had an option like PDO:

PDO::FETCH_ASSOC 

// or

PDO::FETCH_OBJ

I will close issue later, let's leave a little while for viewing. Thanks to all who helped here.

stancl commented 5 years ago

I think you can configure FETCH_ASSOC in some config file.

richbreton commented 5 years ago

I'd just like to add that I ran into this yesterday as well and the difference between the functionality of the two had me puzzled for a decently long time until as I kept getting back an array of objects with toArray() . I finally found this and used the json_decode(toJson(), true) that was suggested but it seems a little hacky to have to convert the result set json and the convert it back to an array when toArray exists. This is a free project and I am very appreciative of all the work that went in and I just wanted to second that this is confusing if you wanted the feedback. I understant that Eloquent implements arrayable (after reading this) but couldn't that be pushed further up the stack (in builder for instance) to have the two operate the same?

andysh-uk commented 4 years ago

I had the same issue and found it confusing that DB::table('...')->where(...)->get() returns objects that cannot be used with DB::table('...')->update() as update() requires an array.

In my case, wrapping the stdObject with get_object_vars($object) in the update function works fine:

$users = DB::table('users')->where('...')->get();

foreach ($users as $user)
{
    $user->set_property = 'new_value';
    DB::table('users')->where('id', $user->id)->update(get_object_vars($user));
}

I know this could really be done using Eloquent models, but I'm doing it in a seeder and I've just preferred to use "low-level" APIs in the seeders to be as efficient as possible, as they could be changing lots of records.

kamleshwebtech commented 3 years ago

If query does not have any record then toArray() does not work on NULL record and returns error.

kolyabres commented 3 years ago
$dogs = Dog::where('adopted', '=', 0)->get()            
            ->map(fn ($el) => (array)$el)
            ->toArray();