'Include data from joined table in update clause - Laravel Eloquent
Good day,
I've been trying to wrap my head around this but can't manage to get this working.
I'm creating an update with a join.
I'm getting data from the second table to update columns with that data in the first table. However I'm running into an issue where I'm unable to access the table alias or table itself from the join in the update statement itself.
Below is the laravel eloquent statement (Please note that I've changed the table names for privacy reasons)
$month = DB::table('table0')->select('month')->first();
DB::table('table1 as table_one')
->join('table2 as table_two', function ($join) {
$join->on('table_two.pno', '=', 'table_one.sno');
$join->on('table_two.sp', '=', 'table_one.sp')
->where('table_one.st', '=', 1);
})
->update([
'c_m' => DB::raw("date_part('minute', \"c_d\")"),
'c_h' => DB::raw("date_part('hour', \"c_d\")"),
'c_w' => DB::raw("date_part('week', \"c_d\")"),
'c_d_s' => DB::raw("date_part('day', \"c_d\")"),
'c_d_l' => DB::raw("date_part('dow', \"c_d\")"),
'b_m' => $month->month,
'c_i' => DB::raw('table_two.c_i'),
'dpt' => DB::raw('table_two.dpt'),
]);
I'm getting the following error: SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "table_two" LINE 1: ..."c_d"), "b_m" = $1, "c_i" = table_two.c_... ^
I've tried various different methods of trying to access data from table_two in the update statement, however I've not been able to find a solution.
I'm using postgresql as database management system.
Any assistance would be greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
