A Referral System With Laravel
Via Composer to add the package to your project's dependencies:
You can click here to go to the original version
$ composer require benahmetcelik/laravel-referral
First add service providers into the config/app.php
\Questocat\Referral\ReferralServiceProvider::class,
Publish the migrations
$ php artisan vendor:publish --provider="Questocat\Referral\ReferralServiceProvider" --tag="migrations"
Publish the config
$ php artisan vendor:publish --provider="Questocat\Referral\ReferralServiceProvider" --tag="config"
Add UserReferral Trait to your User model.
use Questocat\Referral\Traits\UserReferral
class User extends Model
{
use UserReferral;
}
Assigning CheckReferral Middleware To Routes.
// Within App\Http\Kernel Class...
protected $routeMiddleware = [
'referral' => \Questocat\Referral\Http\Middleware\CheckReferral::class,
];
Once the middleware has been defined in the HTTP kernel, you may use the middleware method to assign middleware to a route:
Route::get('/', 'HomeController@index')->middleware('referral');
Now you can create the user:
$user = new App\User();
$user->name = 'zhengchaopu';
$user->password = bcrypt('password');
$user->email = '[email protected]';
$user->save();
// Or
$data = [
'name' => 'zhengchaopu',
'password' => bcrypt('password'),
'email' => '[email protected]',
];
App\User::create($data);
Get the referral link:
$user = App\User::findOrFail(1);
{{ $user->getReferralLink() }}
If you want to change user model this method use (config/refferral.php)
...
'user_model' => 'App\Custom\User\Model',
...
If you want to change ref code lenght this method use (config/refferral.php)
...
'referral_length' => 10,
....
Licensed under the MIT license.