この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:7
ページ更新者:T
更新日時:2026-06-11 07:12:00

タイトル: 条件分岐(@if)
SEOタイトル: Laravel Blade @if 条件分岐の使い方完全ガイド

この記事の要点
  • Blade の条件分岐: @if / @elseif / @else / @endif@unless@empty@isset@switch@auth@guest@can
  • @if は内部的に <?php if(...): ?> にコンパイルされる
  • @isset は変数定義 & 非 null チェック、@empty は変数が空 (PHP empty 同じ)
  • @auth / @guest はログイン状態判定、@can(&quot;edit&quot;, $post) は Gate / Policy 判定
  • 条件付きインクルードは @includeWhen($cond, &quot;view&quot;) または @includeIf

基本: @if / @elseif / @else

{{-- 基本形 --}}
@if ($user->age >= 20)
    <p>成人</p>
@elseif ($user->age >= 13)
    <p>未成年 (中高生)</p>
@else
    <p>子ども</p>
@endif

{{-- コンパイル後は PHP の if --}}
<?php if ($user->age >= 20): ?>
    <p>成人</p>
<?php elseif ($user->age >= 13): ?>
    <p>未成年 (中高生)</p>
<?php else: ?>
    <p>子ども</p>
<?php endif; ?>

@unless: not の糖衣構文

{{-- @if (!...) と同じ意味 --}}
@unless (Auth::check())
    <a href="/login">ログイン</a>
@endunless

{{-- これと等価 --}}
@if (! Auth::check())
    <a href="/login">ログイン</a>
@endif

@isset: 変数定義 & 非 null チェック

{{-- 変数が定義されかつ null でないとき --}}
@isset ($user)
    <p>{{ $user->name }}</p>
@endisset

{{-- @isset は内部的に --}}
<?php if (isset($user)): ?>
    <p>{{ $user->name }}</p>
<?php endif; ?>

{{-- 配列キーの存在チェック --}}
@isset ($data['name'])
    {{ $data['name'] }}
@endisset

@empty: 空判定

{{-- PHP の empty() と同じ --}}
@empty ($users)
    <p>ユーザーは登録されていません</p>
@endempty

{{-- @forelse とセットでよく使う --}}
@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty
    <li>ユーザーがいません</li>
@endforelse

@auth / @guest: ログイン状態判定

{{-- ログイン中のみ表示 --}}
@auth
    <p>{{ Auth::user()->name }} さん、こんにちは</p>
    <a href="/logout">ログアウト</a>
@endauth

{{-- 未ログインのみ表示 --}}
@guest
    <a href="/login">ログイン</a>
    <a href="/register">新規登録</a>
@endguest

{{-- ガード指定 --}}
@auth ('admin')
    <a href="/admin">管理画面</a>
@endauth

@guest ('api')
    <p>API トークンが必要です</p>
@endguest

@can / @cannot: 認可 (Gate / Policy)

{{-- ユーザーが特定の権限を持つか --}}
@can ('update', $post)
    <a href="/posts/{{ $post->id }}/edit">編集</a>
@endcan

@cannot ('delete', $post)
    <p>削除権限がありません</p>
@endcannot

{{-- 複数権限のいずれか --}}
@canany (['update', 'delete'], $post)
    <button>操作メニュー</button>
@endcanany

{{-- これは AuthServiceProvider で定義 --}}
{{-- Gate::define('update', fn($user, $post) => $user->id === $post->user_id); --}}

@switch: 多分岐

@switch ($status)
    @case ('draft')
        <span class="badge gray">下書き</span>
        @break

    @case ('published')
        <span class="badge green">公開</span>
        @break

    @case ('archived')
        <span class="badge red">アーカイブ</span>
        @break

    @default
        <span class="badge">不明</span>
@endswitch

条件付きクラス: @class

Laravel 8.51+ で追加された便利ディレクティブ:

{{-- 条件に応じて class を付与 --}}
<div @class([
    'p-4',
    'bg-red-100 text-red-800' => $hasError,
    'bg-green-100 text-green-800' => $isSuccess,
    'opacity-50' => $isDisabled,
])>
    メッセージ
</div>

{{-- 同様に @style もある --}}
<div @style(['color: red' => $important])>...</div>

{{-- @checked / @selected / @disabled / @readonly --}}
<input type="checkbox" @checked(old('agree', $user->agree))>
<option value="JP" @selected($country === 'JP')>日本</option>
<button @disabled($isLoading)>送信</button>

条件付きインクルード

{{-- 条件 true のときだけインクルード --}}
@includeWhen ($user->isAdmin(), 'admin.menu', ['user' => $user])

{{-- 条件 false のときだけインクルード --}}
@includeUnless ($user->isAdmin(), 'guest.banner')

{{-- ビューファイルが存在すればインクルード --}}
@includeIf ('partials.optional-banner')

{{-- 配列の最初に存在するビューをインクルード --}}
@includeFirst (['custom.header', 'default.header'])

ネストと組み合わせ例

@auth
    @if ($user->isAdmin())
        <a href="/admin">管理</a>
    @elseif ($user->isPaidMember())
        <a href="/premium">プレミアム機能</a>
        @can ('create', App\Models\Post::class)
            <a href="/posts/create">投稿作成</a>
        @endcan
    @else
        <a href="/upgrade">プランをアップグレード</a>
    @endif
@else
    @guest
        <a href="/login">ログイン</a>
    @endguest
@endauth

ディレクティブ一覧

ディレクティブ同等の PHP用途
@if / @endifif {} 真偽判定
@unless / @endunlessif (!...)否定形
@isset / @endissetisset()変数定義チェック
@empty / @endemptyempty()空判定
@auth / @endauthAuth::check()ログイン中のみ
@guest / @endguest!Auth::check()未ログインのみ
@can / @endcanGate::allows()権限あり
@cannot / @endcannotGate::denies()権限なし
@switch / @endswitchswitch {}多分岐
@productionapp()->environment('production')本番のみ
@env('local')app()->environment('local')環境別

FAQ

Q: @if{{ ... ? ... : ... }} どちらを使う?
A: ブロック (タグ複数行) は @if、属性値や 1 つの値なら三項演算子 / null 合体演算子 ??

Q: @if(isset($var))@isset($var) どちらが良い?
A: 短い @isset を推奨。意図が明確で @endisset によりブロック範囲も分かりやすい。

Q: Blade のコンパイル結果を見たい
A: storage/framework/views/.php ファイルが生成される。php artisan view:clear でクリア。