I'm using Laravel 8.x and am trying to get some data from my database. I'm then joining another table, which is fine, except my ID column of my reports table is being overridden by my report_data column, the ID column there.
I've tried to use the ->select('report.*'), which seems to preserve my ID column, but then I don't have my columns from my report_data table included in my left join?
What am I doing wrong?
/**
* View a report
*
* @return Response
*/
public function view($id)
{
$report = DB::table('reports')
->where('id', $id)
->where('user_id', Auth::id())
->first();
if (!empty($report->report_data_id)) {
$report = DB::table('reports')
->where('reports.id', $id)
->where('reports.user_id', Auth::id())
->leftJoin('report_data', 'report_data.id', '=', 'reports.report_data_id')
->first();
$report->discovery_filters = json_decode($report->discovery_filters);
$report->report_data = json_decode($report->report_data);
}
return response()->json([
'success' => true,
'message' => 'Your report',
'report' => $report
], 200);
}
I need to preserve the id column from my reports table.