I want to add elements to each result of a SQL query:
$res = DB::table('users')->get();
forEach ($res as $elem) {
$elem['new_data'] = 'this is my new data';
}
But I get this error on the line in the loop:
Cannot use object of type stdClass as array
How should I do that?
Thank you for your help.
object of type stdClassas anarray. so just replace$elem['new_data']to$elem->new_datasetAttributemethod for that stuff. But for just stdClass I cannot predict, just give it a try.setAttributefunction on its Model class. Well inherited from a trait, HasAttribute. IIRC, though, if you attempt to save that record with the new prop it'll throw a column not found error.