Conditional Methods

Gale provides fluent conditional methods that allow you to build responses dynamically based on conditions, request types, or navigation states. These methods maintain the chainable API while enabling flexible response logic.

The when() Method

Execute a callback when a condition is truthy. Supports both boolean values and callable conditions.

// With boolean condition
return gale()
    ->state('items', $items)
    ->when($user->isAdmin, function ($gale) {
        $gale->state('adminTools', true);
    });

// With callable condition
return gale()
    ->state('cart', $cart)
    ->when(fn() => $cart->isEmpty(), function ($gale) {
        $gale->messages(['_info' => 'Your cart is empty']);
    });

Fallback Callbacks

Provide an optional fallback callback that executes when the condition is falsy:

return gale()
    ->when(
        $user->hasVerifiedEmail,
        // Condition true
        function ($gale) {
            $gale->state('dashboard', true);
        },
        // Condition false (fallback)
        function ($gale) {
            $gale
                ->state('showVerifyBanner', true)
                ->messages(['_warning' => 'Please verify your email']);
        }
    );

The unless() Method

Execute a callback when a condition is falsy. The inverse of when().

return gale()
    ->state('products', $products)
    ->unless($products->count(), function ($gale) {
        $gale->state('showEmptyState', true);
    });

// With fallback
return gale()
    ->unless(
        $user->hasSubscription,
        function ($gale) {
            $gale->state('showUpgradePromo', true);
        },
        function ($gale) {
            $gale->state('premiumFeatures', true);
        }
    );

The whenGale() Method

Execute a callback only for Gale (SSE) requests. Useful for providing different responses to Gale requests vs traditional HTTP requests.

return gale()
    ->whenGale(function ($gale) {
        // Only for Gale requests
        $gale
            ->state('items', $items)
            ->dispatch('items-loaded');
    })
    ->web(view('items.index', compact('items')));

The whenNotGale() Method

Execute a callback only for traditional (non-Gale) HTTP requests:

return gale()
    ->state('data', $data)
    ->whenNotGale(function ($gale) use ($data) {
        // Traditional request - maybe log analytics
        info('Page viewed via direct navigation');
    });

The whenGaleNavigate() Method

Execute a callback for Gale navigate requests. Supports filtering by navigation key for targeted partial updates.

// Any navigate request
return gale()
    ->whenGaleNavigate(function ($gale) {
        $gale->fragment('page', 'content', $data);
    });

// Specific navigation key
return gale()
    ->whenGaleNavigate('sidebar', function ($gale) {
        $gale->fragment('partials.sidebar', '#sidebar', [
            'navItems' => $navItems,
        ]);
    });

// Multiple navigation keys
return gale()
    ->whenGaleNavigate(['main', 'content'], function ($gale) {
        $gale->fragment('page', '#main-content', $data);
    });

Chaining Conditionals

Conditional methods can be chained together for complex logic:

return gale()
    ->state('user', $user)
    ->state('items', $items)

    // Admin-only features
    ->when($user->isAdmin, function ($gale) {
        $gale
            ->state('adminMode', true)
            ->state('pendingApprovals', Item::pending()->count());
    })

    // Show promo unless subscribed
    ->unless($user->isPremium, function ($gale) {
        $gale->state('showUpgradePrompt', true);
    })

    // Navigation-specific fragments
    ->whenGaleNavigate('sidebar', function ($gale) use ($user) {
        $gale->fragment('sidebar', '#sidebar', [
            'user' => $user,
        ]);
    })

    // Web fallback
    ->web(view('dashboard', compact('user', 'items')));

Method Reference

Method Executes When Parameters
when($cond, $cb) Condition is truthy condition, callback, [fallback]
unless($cond, $cb) Condition is falsy condition, callback, [fallback]
whenGale($cb) Request is a Gale request callback, [fallback]
whenNotGale($cb) Request is NOT a Gale request callback, [fallback]
whenGaleNavigate($key, $cb) Navigate request matches key [key], callback, [fallback]

Practical Patterns

Role-Based Responses

public function dashboard()
{
    $user = auth()->user();

    return gale()
        ->state('user', $user)

        ->when($user->hasRole('admin'), function ($gale) {
            $gale->state('stats', [
                'totalUsers' => User::count(),
                'revenue' => Order::sum('total'),
            ]);
        })

        ->when($user->hasRole('manager'), function ($gale) use ($user) {
            $gale->state('teamMembers', $user->team->members);
        })

        ->when($user->hasRole('user'), function ($gale) use ($user) {
            $gale->state('recentOrders', $user->orders()->recent()->get());
        });
}

Multi-Region Page Updates

public function show(Post $post)
{
    return gale()
        // Handle sidebar navigation
        ->whenGaleNavigate('sidebar', function ($gale) use ($post) {
            $gale->fragment('posts.partials.sidebar', '#sidebar', [
                'relatedPosts' => $post->related()->take(5)->get(),
            ]);
        })

        // Handle main content navigation
        ->whenGaleNavigate('main', function ($gale) use ($post) {
            $gale->fragment('posts.partials.content', '#main-content', [
                'post' => $post,
            ]);
        })

        // Handle comments navigation
        ->whenGaleNavigate('comments', function ($gale) use ($post) {
            $gale->fragment('posts.partials.comments', '#comments', [
                'comments' => $post->comments()->with('user')->get(),
            ]);
        })

        // Full page for web fallback
        ->web(view('posts.show', compact('post')));
}

On this page