Get data from a PHP Laravel Object

1

I am listing the data of a query I made with the following code:

$users = DB::table('users')
 ->join('transportes', 'users.id', '=', 'transportes.user_id')
 ->join('empresas', 'transportes.id', '=', 'empresas.transporte_id')
 ->join('motoristas', 'transportes.id', '=', 'motoristas.transporte_id')
 ->join('caminhoes', 'motoristas.id', '=', 'caminhoes.motorista_id')
 ->select('caminhoes.cavalo', 'caminhoes.carreta', 'transportes.data_registro')
 ->whereBetween('transportes.data_registro',['2017-01-01 03:02:46','2018-03-12 11:59:25'])
 ->get(); 

And the following result is coming out:

  

object (Illuminate \ Support \ Collection) # 206 (1) {["items": protected] = >   array (15) {[0] = > object (stdClass) # 213 (3) {["horse"] = > string (7)   "AAA-221" ["cart"] = > string (7) "BBB-196" ["record_data"] = >   string (19) "2018-03-06 22:41:07"} [1] = > object (stdClass) # 209 (3) {   ["horse"] = > string (7) "AAA-221" ["cart"] = > string (7) "BBB-196"   ["record_data"] = > string (19) "2018-03-06 22:41:07"} [2] = >   object (stdClass) # 212 (3) {["horse"] = > string (7) "AAA-221"   ["cart"] = > string (7) "BBB-196" ["record_data"] = > string (19)   "2018-03-06 22:41:07"} [3] = > object (stdClass) # 215 (3) {["horse"] = >   string (7) "AAA-489" ["cart"] = > string (7) "BBB-213"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [4] = >   object (stdClass) # 208 (3) {["horse"] = > string (7) "AAA-489"   ["cart"] = > string (7) "BBB-213" ["data_registration"] = > string (19)   "2018-02-27 01:52:54"} [5] = > object (stdClass) # 207 (3) {["horse"] = >   string (7) "AAA-388" ["cart"] = > string (7) "BBB-309"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [6] = >   object (stdClass) # 210 (3) {["horse"] = > string (7) "AAA-388"   ["cart"] = > string (7) "BBB-309" ["record_name"] = > string (19)   "2018-02-27 01:52:54"} [7] = > object (stdClass) # 216 (3) {["horse"] = >   string (7) "AAA-448" ["cart"] = > string (7) "BBB-209"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [8] = >   object (stdClass) # 217 (3) {["horse"] = > string (7) "AAA-448"   ["cart"] = > string (7) "BBB-209" ["record_data"] = > string (19)   "2018-02-27 01:52:54"} [9] = > object (stdClass) # 218 (3) {["horse"] = >   string (7) "AAA-401" ["cart"] = > string (7) "BBB-271"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [10] = >   object (stdClass) # 219 (3) {["horse"] = > string (7) "AAA-401"   ["cart"] = > string (7) "BBB-271" ["data_registration"] = > string (19)   "2018-02-27 01:52:54"} [11] = > object (stdClass) # 220 (3) {["horse"] = >   string (7) "AAA-187" ["cart"] = > string (7) "BBB-422"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [12] = >   object (stdClass) # 221 (3) {["horse"] = > string (7) "AAA-187"   ["cart"] = > string (7) "BBB-422" ["record_data"] = > string (19)   "2018-02-27 01:52:54"} [13] = > object (stdClass) # 222 (3) {["horse"] = >   string (7) "AAA-276" ["cart"] = > string (7) "BBB-438"   ["record_data"] = > string (19) "2017-12-18 20:35:44"} [14] = >   object (stdClass) # 223 (3) {["horse"] = > string (7) "AAA-276"   ["cart"] = > string (7) "BBB-438" ["record_time"] = > string (19)   "2017-12-18 20:35:44"}}}

I wish I could get the data separated. Example: just pick up the 'horses' column and list?

    
asked by anonymous 25.11.2018 / 13:33

2 answers

0

I was able to solve it, I just made my object into an array with the function toArray()

as follows $array = $users->toArray();

so list as follows:

  

array (15) {[0] = > object (stdClass) # 213 (3) {["horse"] = > string (7)   "AAA-221" ["cart"] = > string (7) "BBB-196" ["record_data"] = >   string (19) "2018-03-06 22:41:07"} [1] = > object (stdClass) # 209 (3) {   ["horse"] = > string (7) "AAA-221" ["cart"] = > string (7) "BBB-196"   ["record_data"] = > string (19) "2018-03-06 22:41:07"} [2] = >   object (stdClass) # 212 (3) {["horse"] = > string (7) "AAA-221"   ["cart"] = > string (7) "BBB-196" ["record_data"] = > string (19)   "2018-03-06 22:41:07"} [3] = > object (stdClass) # 215 (3) {["horse"] = >   string (7) "AAA-489" ["cart"] = > string (7) "BBB-213"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [4] = >   object (stdClass) # 208 (3) {["horse"] = > string (7) "AAA-489"   ["cart"] = > string (7) "BBB-213" ["data_registration"] = > string (19)   "2018-02-27 01:52:54"} [5] = > object (stdClass) # 207 (3) {["horse"] = >   string (7) "AAA-388" ["cart"] = > string (7) "BBB-309"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [6] = >   object (stdClass) # 210 (3) {["horse"] = > string (7) "AAA-388"   ["cart"] = > string (7) "BBB-309" ["record_name"] = > string (19)   "2018-02-27 01:52:54"} [7] = > object (stdClass) # 216 (3) {["horse"] = >   string (7) "AAA-448" ["cart"] = > string (7) "BBB-209"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [8] = >   object (stdClass) # 217 (3) {["horse"] = > string (7) "AAA-448"   ["cart"] = > string (7) "BBB-209" ["record_data"] = > string (19)   "2018-02-27 01:52:54"} [9] = > object (stdClass) # 218 (3) {["horse"] = >   string (7) "AAA-401" ["cart"] = > string (7) "BBB-271"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [10] = >   object (stdClass) # 219 (3) {["horse"] = > string (7) "AAA-401"   ["cart"] = > string (7) "BBB-271" ["data_registration"] = > string (19)   "2018-02-27 01:52:54"} [11] = > object (stdClass) # 220 (3) {["horse"] = >   string (7) "AAA-187" ["cart"] = > string (7) "BBB-422"   ["record_data"] = > string (19) "2018-02-27 01:52:54"} [12] = >   object (stdClass) # 221 (3) {["horse"] = > string (7) "AAA-187"   ["cart"] = > string (7) "BBB-422" ["record_data"] = > string (19)   "2018-02-27 01:52:54"} [13] = > object (stdClass) # 222 (3) {["horse"] = >   string (7) "AAA-276" ["cart"] = > string (7) "BBB-438"   ["record_data"] = > string (19) "2017-12-18 20:35:44"} [14] = >   object (stdClass) # 223 (3) {["horse"] = > string (7) "AAA-276"   ["cart"] = > string (7) "BBB-438" ["record_time"] = > string (19)   "2017-12-18 20:35:44"}}

With this I can get the data .. eg:

echo $array[0]->cavalo

result:

  

string (7) "AAA-221"

    
25.11.2018 / 13:46
0

Hello,

The get() method returns a Collection which is an object with several useful methods to apply over the collection of returned data.

To get a single column of all items in your collection you can use the pluck('nome_da_coluna') method, for example $users->puck('cavalo') will return a Collection with the horse column values:

#items: array:15 [▼
    0 => "AAA-221"
    1 => "AAA-221"
    2 => "AAA-221"
    ...
  ]

The documentation of the pluck method and other methods can be found here: link

You can also iterate over a collection as usual:

foreach($users as $user){
    echo $user->cavalo . PHP_EOL;
    echo $user->carreta . PHP_EOL;
}
    
25.11.2018 / 22:21