Giter VIP home page Giter VIP logo

larainvite's Introduction

larainvite

User (signup) invitation package for laravel

GitHub license Total Downloads

larainvite is a laravel package, to allow existing users to invite others by email.

It generates referral code and keep track of status.

Installation

Begin by installing the package through Composer. Run the following command in your terminal:

composer require junaidnasir/larainvite

add the package service provider in the providers array in config/app.php:

Junaidnasir\Larainvite\LaraInviteServiceProvider::class

you may add the facade access in the aliases array:

'Invite'  => Junaidnasir\Larainvite\Facades\Invite::class

publish the migration and config file:

php artisan vendor:publish"

migrate to create user_invitation table

php artisan migrate

edit your User model to include larainviteTrait

use Junaidnasir\Larainvite\InviteTrait;
class user ... {
    use InviteTrait;
}

Usage

You can use facade accessor to retrieve the package controller. Examples:

$user = Auth::user();
//Invite::invite(EMAIL, REFERRAL_ID); 
$refCode = Invite::invite('[email protected]', $user->id);
//or 
//Invite::invite(EMAIL, REFERRAL_ID, EXPIRATION); 
$refCode = Invite::invite('[email protected]', $user->id, '2016-12-31 10:00:00');
//or
//Invite::invite(EMAIL, REFERRAL_ID, EXPIRATION, BEFORE_SAVE_CALLBACK); 
$refCode = Invite::invite($to, Auth::user()->id, Carbon::now()->addYear(1),
                      function(/* InvitationModel, see Configurations */ $invitation) use ($someValue) {
      $invitation->someParam = $someValue;
});

now create routes with refCode, when user access that route you can use following methods

// Get route
$code = Request::input('code');
if( Invite::isValid($code))
{
    $invitation = Invite::get($code); //retrieve invitation modal
    $invited_email = $invitation->email;
    $referral_user = $invitation->user;

    // show signup form
} else {
    $status = Invite::status($code);
    // show error or show simple signup form
}
// Post route
$code = Request::input('code');
$email = Request::input('signup_email');
if( Invite::isAllowed($code,$email) ){
    // Register this user
    Invite::consume($code);
} else {
    // either refCode is inavalid, or provided email was not invited against this refCode
}

with help of new trait you have access to invitations sent by user

$user= User::find(1);
$invitations = $user->invitations;
$count = $user->invitations()->count();

Events

larainvite fires several events

  • 'junaidnasir.larainvite.invited'
  • 'junaidnasir.larainvite.consumed'
  • 'junaidnasir.larainvite.canceled'
  • 'junaidnasir.larainvite.expired'

all of these events incloses invitation modal so you can listen to these events. include listener in EventServiceProvider.php

protected $listen = [
    'junaidnasir.larainvite.invited' => [
        'App\Listeners\userInvite',
    ],
];

userInvite.php

public function handle($invitation)
{
    \Mail::queue('invitations.emailBody', $invitation, function ($m) use ($invitation) {
            $m->from('From Address', 'Your App Name');
            $m->to($invitation->email);
            $m->subject("You have been invited by ". $invitation->user->name);
        });
}

Configurations

in config/larainvite.php you can set default expiration time in hours from current time.

'expires' => 48

you can also change user model to be used, in larainvite.php

'UserModel' => 'App\User'

you can also change invitation model to be used, in larainvite.php

'InvitationModel' => 'App\Invitation'

larainvite's People

Contributors

junaidnasir avatar bmichotte avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.