2 回答

TA貢獻1812條經驗 獲得超5個贊
public function save(Request $request)
{
try{
Ticket::create([
'user_id' => Laptop::where('SN', $request->SN)->first()->user_id,
'laptop_id' => Laptop::where('SN', $request->SN)->first()->id,
'title' => $request->title,
'body' => $request->body,
'laptop_ww' => $request->laptop_ww
return view(your_address, "successfully saved");
]);
}catch(Exception ex){
return view(your_address, "Custom error here");
}
}

TA貢獻1862條經驗 獲得超7個贊
試試下面 -
$laptop = Laptop::where('SN', $request->SN)->first();
if(null === $laptop){
throw new Exception('No laptop found with SN'.$request->SN);
}
if(null === $laptop->user_id){
throw new Exception('Laptop with SN:'.$request->SN.' does has any user'.);
}
Ticket::create([
'user_id' => $laptop->user_id,
'laptop_id' => $laptop->id,
'title' => $request->title,
'body' => $request->body,
'laptop_ww' => $request->laptop_ww
]);
您將保存查詢并處理錯誤。
- 2 回答
- 0 關注
- 125 瀏覽
添加回答
舉報