2 回答

TA貢獻1818條經(jīng)驗 獲得超8個贊
這是一個簡單的 SQL 查詢,但在原始 SQL 中執(zhí)行比通過內(nèi)置查詢構(gòu)建器更容易:
$userId = 2; // id of chris
$sql = <<<SQL
UPDATE u
SET u.vehicle_desc = v.vehicle
FROM users u
INNER JOIN vehicles v ON v.id = u.vehicle_id
WHERE u.id = ?
SQL;
\DB::statement($sql, [$userId]);

TA貢獻1804條經(jīng)驗 獲得超3個贊
如果您想在最終用戶從前端更新車輛表后立即更改用戶,那么我建議您在使用分配給 Model Vehicle 的控制器時,例如 VehicleController,創(chuàng)建一個更新函數(shù)。假設(shè)您在 Vehicle 表中只有一個名為 Vehicle 的列。
//Inside Vehicle Controller
use App\User; \\call the User model
use App\Vehicle; \\call the Vehicle model
假設(shè)用戶從表單更新了 ID 為 202 的車輛,然后在視圖中創(chuàng)建一個表單并在其中發(fā)布帶有 202 值的 ID,例如:
<form method="post" action="{{ url('vehicle/'.$id) }}" >
@csrf
your form inputs and submit button
</form>
然后使用路由將表單發(fā)布到 VehicleController@update (或者您可以使用資源控制器)例如 Route::post('vehicle/{id}','VehicleController@update'); 然后在 Vehicle Controller 中
public function update(Request $request, $id)
{
$request->validate([
//provide your validation here
]);
$vehicle_req = $request['name_of_input field'];
//After validation
$vehicle = Vehicle::find($id);
$user = User::find($id);
$vehicle->update([
'Vehicle' => $vehicle_req,
]);
$user->update([
'vehicle_Desc' => $vehicle_req,
]);
return redirect()->back()->with('success', 'Value Edited
successfully.');
}
最后,您可以在視圖上顯示成功消息。它將同時更新 Vehicle 表的 Vehicle Column 和 User 表的 Vehicle_Desc 。
- 2 回答
- 0 關(guān)注
- 177 瀏覽
添加回答
舉報