0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】500エラーページに飛ばす

Posted at

概要

Laravelで500エラーページに強制的に飛ばす方法をメモ。

結論を言うと、コントローラーで1行書くだけでページに飛ばす事ができる。

abort(500);

こちらを記述すると下記のようなページに飛ぶ。
te.png

使用例

例えばAuth::user()などで、ログイン中のユーザーから値を取る際、ログイン中のユーザーが管理者の権限で削除されたり、存在しない状態となった場合、WEBアプリが開発者用のエラーを吐いてしまう。

これを防止するために、コントローラーのメソッドの先頭にabort(500);を利用する。

// ユーザー情報が存在するか確認
        if (empty(Auth::user()->id)) {
            abort(500);
        }

これでユーザー情報が存在しない場合、500ページに飛ばす事ができる。
ちなみにabort(404);などで他のエラーページを表示する事も可能。

abort(404);

jsonの中に入れることもできる。

return response()->json(['message' => 'error'], 500);
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?