Hello everyone, thank you very much in advance for your answers and comments. The question is the following. I want to add another key and value to the object that returns a query. I have the following query using Laravel's Query Builder.
$query = DB::table($table)->get()->toArray();
This query returns the following object:
1: {id: 1, genero: "femenino", visible: "femenino"}
2: {id: 2, genero: "masculino", visible: "masculino"}
Now I want to add to the object that you have returned a new key and value, remaining as follows:
1: {id: 1, genero: "femenino", visible: "femenino", table: 'genero' }
2: {id: 2, genero: "masculino", visible: "masculino", table: 'genero' }
I have the name of the table that I want to add stored in a variable $table in the following way:
$table = 'genero';
$query = DB::table($table)->get()->toArray();
How can I add the name of the table the records come from?
I appreciate your responses and comments by the way I can not use eloquent to generate the query.
you can do a foreach to add to each of the objects in the array
where
$value->table
is the key, and$table
is the value you add.What I would normally do when I need the same static value in each row of a query is the following:
I don't really know which answer is more efficient.