I am working to understand joins in Laravel 6 and the names of the retrieved columns from a join. I have built a meaningless test data set and am trying different queries. I am having trouble getting back columns from the joined table.
I have 2 tables - toplevels and lowerlevels. I have the following query:
$main_join = DB::table('toplevels')->join('lowerlevels', 'toplevels.other', '=', 'lowerlevels.id')->select('toplevels.*', 'lowerlevels.*');
I have used the toSql method to see the resulting SQL query and it is correct:
select `toplevels`.*, `lowerlevels`.* from `toplevels` inner join `lowerlevels` on `toplevels`.`other` = `lowerlevels`.`id`
I run the query using the first() method -- $results = $main_join->first();
--, then I am trying to retrieve the values in the columns.
Trying to obtain the 'id' field of the 'toplevels' table via $tl_id = $results['toplevels.id'];
generates php errors so I did a var_export on the $results variable and this is what that shows:
array(
'id' => 36,
'created_at' => '2020-05-02 21:06:08',
'updated_at' => '2020-05-02 21:06:08',
'my_name' => '0091e53e46dadbf556fa03c06c84f61c2feaa4a7',
'other' => 36,
'val1' => '18463.65',
'val2' => '41455.81',
)
So, that is a dump just of the values in the toplevels record, without the lowerlevels value, and the names of the columns are the original names from the toplevels table with no indication of any of the column names from the lowerlevels table.
So I am confused how to obtain and reference the columns from the second table.
Any suggestions?
Thank you.