PHP code example of splitstack / laravel-nudge

1. Go to this page and download the library: Download splitstack/laravel-nudge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

splitstack / laravel-nudge example snippets


use Splitstack\Nudge\NudgeAction;

class InstallGitHubApp extends NudgeAction
{
    public function actionKey(): string
    {
        return 'github.install';
    }

    protected function nudge(int $user_id, int $installation_id): mixed
    {
        // your logic here — no event dispatch needed
    }
}

use Splitstack\Nudge\Attributes\Nudge;
use Splitstack\Nudge\NudgeAction;

class InstallGitHubApp extends NudgeAction
{
    public function actionKey(): string
    {
        return 'github.install';
    }

    #[Nudge]
    protected function install(int $user_id, int $installation_id): mixed
    {
        // your logic here
    }
}

// Global helper
nudge(InstallGitHubApp::class, ['user_id' => $user->id]);

// Facade
use Splitstack\Nudge\Facades\Nudge;
Nudge::run(InstallGitHubApp::class, ['user_id' => $user->id]);
// or 
Nudge::run($myActionInstance, ['user_id' => $user->id]);

// Direct call — use named args
$action->handle(user_id: $user->id, installation_id: 88);

use Splitstack\Nudge\Concerns\DispatchesActionExecuted;
use Splitstack\Nudge\Contracts\ResolvableAction;

class InstallGitHubApp implements ResolvableAction
{
    use DispatchesActionExecuted;

    public function actionKey(): string // Add this method to declare the action key that resolves notifications
    {
        return 'github.install';
    }

    public function doOrDoNotThereIsNoTry(string $action, array $installation, array $repositories): mixed
    {
        // your existing logic, untouched

        $this->nudge(installation_id: $installation['id']); // ← only addition; pass only what's needed for matching

        return $result;
    }
}

// call site — completely unchanged
(new InstallGitHubApp)->doOrDoNotThereIsNoTry($action, $installation, $repositories);

use Splitstack\Nudge\Events\ActionExecuted;

ActionExecuted::dispatch('github.install', ['user_id' => $user->id]);

$user->notify(
    (new GitHubSetupReminder)->forAction('github.install', ['user_id' => $user->id])
);

class AppNotification extends ActionableNotification
{
    public function __construct(
        public readonly string $message,
        public readonly ?string $actionKey = null,
    ) {}

    protected function withData(object $notifiable): array
    {
        return ['message' => $this->message];
    }

    public function useActionKey(): ?string
    {
        return $this->actionKey;
    }
}

// sending (no params):
$user->notify(new AppNotification('Connect your GitHub account.', 'github.install'));

// sending (with params):
$user->notify(
    (new AppNotification('Connect your GitHub account.', 'github.install'))
        ->withParams(['user_id' => $user->id])
);

// stored:   ['user_id' => 5]
// executed: ['user_id' => 5, 'installation_id' => 88]
// → resolves ✓

// stored:   ['user_id' => 5]
// executed: ['user_id' => 9]
// → does not resolve ✗

// config/nudge.php
'match_params' => 'deep',

// stored:   ['installation' => ['id' => 88]]
// executed: ['installation' => ['id' => 88, 'account' => 'acme'], 'user_id' => 5]
// shallow → does not resolve ✗  (array !== array strict comparison)
// deep    → resolves ✓          (['id' => 88] is a subset of ['id' => 88, 'account' => 'acme'])

use Splitstack\Nudge\Models\Concerns\HasResolvableNotifications;

class User extends Authenticatable
{
    use HasResolvableNotifications;
}

$user->pendingNotifications()->get();
$user->resolvedNotifications()->get();

use Illuminate\Notifications\DatabaseNotification;

DatabaseNotification::whereNull('resolved_at')->get();
DatabaseNotification::whereNotNull('resolved_at')->get();

// before — toDatabase() override will break resolution
class MyNotification extends ActionableNotification
{
    public function toDatabase(object $notifiable): array
    {
        return ['message' => 'Do the thing.'];
    }
}

// after
class MyNotification extends ActionableNotification
{
    protected function withData(object $notifiable): array
    {
        return ['message' => 'Do the thing.'];
    }
}

// risky: controller may be called more than once
public function dashboard(Request $request): Response
{
    $request->user()->notify(new SomeReminder); // could fire twice
    return Inertia::render('Dashboard');
}

// safer: notification lives outside the repeatedly-hit method
public function dashboard(Request $request): Response
{
    SendOnboardingReminder::dispatchIf(
        ! $request->user()->hasCompletedOnboarding()
    );
    return Inertia::render('Dashboard');
}
bash
php artisan vendor:publish --tag=nudge-migrations
php artisan migrate