亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在錯誤頁面上顯示表單錯誤并在 Laravel 中進行驗證

在錯誤頁面上顯示表單錯誤并在 Laravel 中進行驗證

PHP
嚕嚕噠 2022-07-16 09:49:07
如果$request->SN無效或為空,我會收到如下錯誤。試圖獲取非對象的屬性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]);如何顯示自定義錯誤而不是此異常?默認情況下,它會轉到狀態為 500 的錯誤頁面。
查看完整描述

2 回答

?
慕雪6442864

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");

    }

  }


查看完整回答
反對 回復 2022-07-16
?
牧羊人nacy

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

        ]);

您將保存查詢并處理錯誤。


查看完整回答
反對 回復 2022-07-16
  • 2 回答
  • 0 關注
  • 125 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號