Taking data from an Laravel PHP Object

Asked

Viewed 1,224 times

2

I am listing from 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" ["data_record"]=> string(19) "2018-03-06 22:41:07" } [1]=> Object(stdClass)#209 (3) { ["horse"]=> string(7) "AAA-221" ["cart"]=> string(7) "BBB-196" ["data_record"]=> string(19) "2018-03-06 22:41:07" } [2]=> Object(stdClass)#212 (3) { ["horse"]=> string(7) "AAA-221" ["cart"]=> string(7) "BBB-196" ["data_record"]=> string(19) "2018-03-06 22:41:07" } [3]=> Object(stdClass)#215 (3) { ["horse"]=> string(7) "AAA-489" ["cart"]=> string(7) "BBB-213" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [4]=> Object(stdClass)#208 (3) { ["horse"]=> string(7) "AAA-489" ["cart"]=> string(7) "BBB-213" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [5]=> Object(stdClass)#207 (3) { ["horse"]=> string(7) "AAA-388" ["cart"]=> string(7) "BBB-309" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [6]=> Object(stdClass)#210 (3) { ["horse"]=> string(7) "AAA-388" ["cart"]=> string(7) "BBB-309" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [7]=> Object(stdClass)#216 (3) { ["horse"]=> string(7) "AAA-448" ["cart"]=> string(7) "BBB-209" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [8]=> Object(stdClass)#217 (3) { ["horse"]=> string(7) "AAA-448" ["cart"]=> string(7) "BBB-209" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [9]=> Object(stdClass)#218 (3) { ["horse"]=> string(7) "AAA-401" ["cart"]=> string(7) "BBB-271" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [10]=> Object(stdClass)#219 (3) { ["horse"]=> string(7) "AAA-401" ["cart"]=> string(7) "BBB-271" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [11]=> Object(stdClass)#220 (3) { ["horse"]=> string(7) "AAA-187" ["cart"]=> string(7) "BBB-422" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [12]=> Object(stdClass)#221 (3) { ["horse"]=> string(7) "AAA-187" ["cart"]=> string(7) "BBB-422" ["data_record"]=> string(19) "2018-02-27 01:52:54" } [13]=> Object(stdClass)#222 (3) { ["horse"]=> string(7) "AAA-276" ["cart"]=> string(7) "BBB-438" ["data_record"]=> string(19) "2017-12-18 20:35:44" } [14]=> Object(stdClass)#223 (3) { ["horse"]=> string(7) "AAA-276" ["cart"]=> string(7) "BBB-438" ["data_record"]=> string(19) "2017-12-18 20:35:44" } } }

i wish I could take the data separately. Example: take only the column 'horses' and list?

2 answers

1

I managed to solve, just turned my object into an array with the function toArray()

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

so listed as follows:

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

with this I can get the data.. ex.:

echo $array[0]->cavalo

outworking:

string(7) "AAA-221"

  • I would remove from select 'trucks.cart', 'transport.data_registration' if you are not using and Join in companies.

  • This solution is bad, it brings data from the table that you do not need (or that at least you did not report the utility). No select only bring what you will actually use.

  • All right, thank you! I’m making the adjustments :D

  • I need all this data, I did according to the table you asked me to generate.

0

Hello,

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

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

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

Documentation of the method pluck and other methods can be found here: https://laravel.com/docs/5.7/collections#method-Pluck

You can also iterate over a collection normally:

foreach($users as $user){
    echo $user->cavalo . PHP_EOL;
    echo $user->carreta . PHP_EOL;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.