2
0

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 1 year has passed since last update.

Laravel,livewire2.0 TipsAdvent Calendar 2022

Day 8

【Laravel】碇くんがもうabort()をする為にif文で囲まなくていいようにする

Last updated at Posted at 2022-12-07

概要

LaravelでHTTP例外を簡単に投げられるヘルパーabort()

これを使う際に大体条件分岐で発動させたりしてると思います。

if($hoge === 'hoge') {
    abort(404);
}

abort_if()

上の例がこう書き直せます

abort_if($hoge === 'hoge', 404);

第一引数に書いた条件がtrueの際に
第二引数で指定したHTTP例外を投げます。

abort_unless()

上の例がこう書き直せます

abort_unless($hoge !== 'hoge', 404);

第一引数に書いた条件がfalseの際に
第二引数で指定したHTTP例外を投げます。

使い分け

〇〇だったら → abort_if()
〇〇に当てはまらなかったら → abort_unless()

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?