PHP code example of rubik-llc / laravel-invite

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

    

rubik-llc / laravel-invite example snippets


// Make an invitation
Invitation::to('[email protected]')->make();

$user = User::find(1);

// Make an invitation directly from an Eloquent Model
$user->invitation()->to('[email protected]')->make();

$referer = User::find(1);

$invitee = User::find(2);

// Set properties of an invitation
Invitation::to('[email protected]')
            ->referer($referer)
            ->invitee($invitee)
            ->expireIn(3, 'days')
            ->make();

// Accept an invitation
Invitation::findByToken('1234')->accept();

return [

    /*
    |--------------------------------------------------------------------------
    | Invitation class
    |--------------------------------------------------------------------------
    |
    | The invite class that should be used to store and retrieve the invitations.
    | If you specify a different model class, make sure that model extends the default
    | Invitation model that is shipped with this package.
    |
    */

    'invitation_model' => \Rubik\LaravelInvite\Models\Invitation::class,

    /*
    |--------------------------------------------------------------------------
    | Delete on decline
    |--------------------------------------------------------------------------
    |
    | When this option is enabled, whenever an invitation is declined it will automatically
    | be deleted.
    |
    */

    'delete_on_decline' => false,

    /*
    |--------------------------------------------------------------------------
    | Unit
    |--------------------------------------------------------------------------
    |
    | The unit of the values.
    | This package uses Carbon for date and time related calculations, therefore
    | the value of this option should be only values that Carbon accepts.
    | e.g: seconds, minutes, hours, days, weeks, months, years, etc.
    |
    */

    'unit' => 'hours',

    /*
    |--------------------------------------------------------------------------
    | Expire
    |--------------------------------------------------------------------------
    | The default value of when to expire an invitation after its created. It uses
    | the units that are specified above.
    |
    | If the delete.auto value is set to true, it enables a scheduler that executes
    | a command every hour which deletes all invitations that have surpassed the amount
    | of time given in delete.after
    |
    */
    'expire' => [

        'after' => 48,

        'delete' => [
            'auto' => false,
            'after' => 48,
        ],

    ],

];

use Rubik\LaravelInvite\Facades\Invitation;

Invitation::to('[email protected]')->make();

$user = User::find(1);

$invitation = $user->invitation()->to('[email protected]')->make();

$invitation->referable // will return the user that made the invitation.

$referer = User::find(1);

Invitation::to('[email protected]')->referer($referer)->make();

$invitation = Invitation::find(1);

$invitation->referer($anotherReferer);

$invitee = User::find(1);

// Using the Facade
$invitation = Invitation::to('[email protected]')->invitee($invitee)->make();

// Using the Referer Model
$invitation = $user->invitation()->to('[email protected]')->invitee($invitee)->make();

$invitation->invitable // will return an instance of the User model

$invitation = Invitation::find(1);

$invitation->invitee($anotherInvitee);

// Using the Facade
$invitation = Invitation::to('[email protected]')->invitee(User::class)->make();

// Using the Referer Model
$invitation = $user->invitation()->to('[email protected]')->invitee($invitee)->make();

$invitation->invitable_type // will return App/Models/User

use Carbon\Carbon;

// Using the Facade
$invitation = Invitation::to('[email protected]')->expireAt('2022-02-02')-make(); //$invitation->expires_at will return '2022-02-02 00:00:00'
$invitation = Invitation::to('[email protected]')->expireAt('2022-02-02 12:50:30')-make(); //$invitation->expires_at will return '2022-02-02 12:50:30'
$invitation = Invitation::to('[email protected]')->expireAt(Carbon::parse('2022-01-01'))-make(); //$invitation->expires_at will return '2022-01-01 00:00:00'

// Using the Referer Model
$invitation = $user->invitation()->to('[email protected]')->expireAt('2022-02-02')-make(); //$invitation->expires_at will return '2022-02-02 00:00:00'
$invitation = $user->invitation()->to('[email protected]')->expireAt('2022-02-02 12:50:30')-make(); //$invitation->expires_at will return '2022-02-02 12:50:30'
$invitation = $user->invitation()->to('[email protected]')->expireAt(Carbon::parse('2022-01-01'))-make(); //$invitation->expires_at will return '2022-01-01 00:00:00'


use Carbon\Carbon;

// Using the Facade
$invitation = Invitation::to('[email protected]')->expireIn(48, 'hours')-make(); //$invitation->expires_at will return now() + 48 hours
$invitation = Invitation::to('[email protected]')->expireIn(10, 'days')-make(); //$invitation->expires_at will return now() + 10 days

// Using the Referer Model
$invitation = $user->invitation()->to('[email protected]')->expireIn(15, 'minutes')-make();  //$invitation->expires_at will return now() + 15 minutes
$invitation = $user->invitation()->to('[email protected]')->expireIn(2, 'months')-make();  //$invitation->expires_at will return now() + 2 months


Invitation::findByToken('1234');

$invitation->accept();

$invitation->decline();

$invitation->decline();

$invitation //null

// config/invite.php

return [
     ...
    
    'invitation_model' => App\Models\CustomInvitation::class,  
     
     ...
]
bash
php artisan vendor:publish --tag="invite-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="invite-config"
 php
namespace App\Models;
use Rubik\LaravelInvite\Models\Invitation;

class CustomInvitation extends Invitation
{   
    ...
}