I'm trying to fetch data from dynamically different tables depending on which user is logged on and if he is allowed to edit said table, what I thought was going to work was this:
$colaboradores = Colaboradore::where('email', '=', Auth::user()->email)->first();
$tables = DB::connection('mysql2')->select("SHOW TABLES LIKE 'intervencoes\_%'");
foreach ($tables as $object) {
$arrays[] = (array)$object;
}
foreach ($arrays as $array) {
$string = implode('', $array);
$test = $colaboradores->Niveis->$string->id;
}
And it outputs an error on the last bit of code saying
"Trying to get property 'id' of non-object"
I have been looking around for hours but couldn't find anything related.
This is all inside a livewire component by the way.
Thanks in advance.
Edit:
Since $colaboradores->Niveis->$string
returns a Collection, you need to iterate over it to get results.
$test = [];
foreach ($colaboradores->Niveis->$string as $obj) {
$test[] = $obj->id;
}
Then you can check the IDs in $test
with dd($test);
for example.
This can cause a problem if $obj
is not a real object, so you could check it with:
if (!is_object($obj)) {
//Do something - Not an object!
}