Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access variable of stdClass Object

I am using Laravel with a select statement to select the row with the highest id like this:

$user_id = DB::connection('mysql2')->select('SELECT MAX(id) FROM users')[0];

This returns an array with an object that looks like this:

stdClass Object ( [MAX(id)] => 11 ) 1

I have tried $object->MAX(id) and $object['MAX(id)'] but it does not seem to work.

like image 900
Karl Eriksson Avatar asked Nov 18 '25 08:11

Karl Eriksson


1 Answers

Dynamic attributes with usually not allowed characters can be accessed using curly brackets like this:

$object->{"MAX(id)"}
like image 162
Fabian Schmengler Avatar answered Nov 19 '25 21:11

Fabian Schmengler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!