Hello everyone, this time I come to you for this problem that I have encountered and I have not been able to solve, I have the following code, which generates queries to various tables in my database, the names of the tables, I store them in a variable for later with a forEach loop generate the data as follows:
public function getCustomData($id)
{
try {
$tables = CatAdministrator::where('cat_modules_id', $id)->pluck('table_name');
// Tables contiene ['tabla1','tabla2'];
$data = null;
foreach($tables as $table) {
$data .= DB::table($table)->get();
}
return response()->json([
'success' => true,
'data' => $data
]);
}catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => $e->getMessage()
]);
}
}
The problem is that this generates the following structure in $data
$data = [
{"id":1,"genero":"Masculino"},
{"id":2,"genero":"Femenino"},
{"id":3,"genero":"Indefinido"}
][
{"id":1,"puestos":"Desarrollador Full Stack Developer"}
];
How can I remove the brackets from the second table so that I have a structure like the following?
$data = [
{"id":1,"genero":"Masculino"},
{"id":2,"genero":"Femenino"},
{"id":3,"genero":"Indefinido"},
{"id":1,"puestos":"Desarrollador Full Stack Developer"}
];
I thank you in advance for your answers and comments, and if anyone knows of a better way than mine to query multiple tables from an array with Laravel's QueryBuilder and return a better data structure, I'm open to receiving opinions. Without further ado, I thank you for your support.
Good day,
You can use array_merge to create a single array
Note that array_merge uses array parameters, so you must convert the query to your database to array, if you add toArray() to the query it should work