LoginSignup
22
30

More than 3 years have passed since last update.

【Laravel】bladeで変数の存在確認する方法

Last updated at Posted at 2020-04-15

「0」,「null」,「""」 をどうとらえるか↓↓↓

関数 / 項目 $a = 未定義 $a = NULL $a = ”” $a = 0 array();
$a == Null true true true true
$a == “” true true true true
$a == 0 true true true true
$a === Null true true false false
$a === “” false false true false
$a === 0 false false false true false
is_null($a) true true false false false
isset($a) false false true true true
empty($a) true true true true true
!empty($a) false false false false false

$textがnullの場合Helloが表示される


@if(isset( $text ))
<p>$test</p>
@else
<p>Hello</p>
@endif

「!」をつけると存在が確認できない場合trueとなる


@if(!isset( $text ))
@if(!is_null($text))

返り値のCollectionの配列が空かの判定にはisEmptyを使う
ドキュメントはこちら


if($a->isEmpty()){
   //$aが空の場合の処理
}else{
   //$aが空じゃない場合の処理
}

ちなみに、、、->first()で取得したCollectionが空の場合NULLが返ってくる

22
30
1

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
22
30