LaravelのCollectionが空かどうか判定するとき
1605 ワード
地味なミスをしていたので備忘録。
失敗ケース
isset($collection)
, empty($collection)
などでは判定ができません。
試しに
{{ var_dump($collection) }}
とするとわかりますが、
object(Illuminate\Database\Eloquent\Collection)#1533 (1) { ["items":protected]=> array(0) { } }
これはただの配列ではなく、 Collectionですもんね。
当たり前のことを忘れていました。
調整
ちゃんとisEmpty()
というCollectionのメソッドを使います。
https://laravel.com/docs/8.x/collections#method-isempty
$collection->isEmpty()
余談
Collectionのメソッドとプロパティ多すぎるですぜ...!
Author And Source
この問題について(LaravelのCollectionが空かどうか判定するとき), 我々は、より多くの情報をここで見つけました https://qiita.com/irico/items/a28d7c9658a6e999562a著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .