PHP code example of multek / laravel-nps

1. Go to this page and download the library: Download multek/laravel-nps 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/ */

    

multek / laravel-nps example snippets


use Multek\Nps\Traits\HasNpsResponses;

class User extends Authenticatable
{
    use HasNpsResponses;
}

use Multek\Nps\Models\NpsSurvey;

$survey = NpsSurvey::create([
    'name' => 'Q1 2026',
    'description' => 'Quarterly NPS survey',
    'is_active' => true,
]);

$user->canAnswerNps(); // checks cooldown, dismissals, eligibility
$user->canAnswerNps($survey); // for a specific survey

$response = $user->answerNps($survey, 9, 'Great service!', ['source' => 'web']);

$user->dismissNps($survey); // records dismissal with attempt tracking

use Multek\Nps\Facades\Nps;

Nps::currentSurvey();           // active survey or null
Nps::forUser($user);            // survey data if eligible, null otherwise
Nps::respond($surveyId, 9);     // record response for auth user
Nps::score($survey);            // NPS score (-100 to 100)
Nps::stats($survey);            // {score, total, promoters, passives, detractors}

public function share(Request $request): array
{
    return [
        ...parent::share($request),
        'nps' => fn () => \Multek\Nps\Facades\Nps::forUser($request->user()),
    ];
}

'routes_enabled' => true,

use Multek\Nps\Events\NpsResponseSubmitted;

Event::listen(NpsResponseSubmitted::class, function ($event) {
    if ($event->response->is_detractor) {
        // Alert customer success team
    }
});
bash
php artisan vendor:publish --tag=nps-config
php artisan vendor:publish --tag=nps-migrations
php artisan migrate