Skip to content

Commit

Permalink
Upgrading package, fix bugs, adding new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
melogail committed Oct 7, 2023
1 parent 0735606 commit f5e1418
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 21 deletions.
74 changes: 65 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To publish the `referrals.php` config file use the `publish` command:
```
$ php artisan vendor:publish
```
and from the providers list choose the `ReferralsLaravelServiceProvider`
and from the providers list choose the `ReferralsLaravelServiceProvider`.

Next, inside your `.env` file, add the route you want the invited user to be redirected to.
```dotenv
Expand All @@ -25,7 +25,7 @@ By default, the invited user will be redirected to the registration page with ro
If you plan to use cookies to save the referral token, by default, the cookie life-time is set to 24 hours. You can customize
the cookie life-time by setting the `referral_token_cookie_lifetime` inside your `.env` file.
```dotenv
referral_token_cookie_lifetime=60*24
referral_token_cookie_lifetime=1440
```

## Usage:
Expand All @@ -38,9 +38,8 @@ class User extends Authenticatable implements MustVerifyEmail{

}
```

Next, use `php artisan migrate` to migrate the database file. This migration will update the database default `users` table
and add two new columns `referral_token` and `referrer_id`.
Next, use `php artisan migrate` to migrate the database file. This migration wll create new table `referrals`. A one-to-many relationship
is created with the users table.

The `referrer_id` is the id of the user who sent the invitation link.

Expand Down Expand Up @@ -68,16 +67,73 @@ protected $middlewareGroups = [
]
```

Add the two new columns inside your `User` model in the `protected $fillable` property along with other
attributes:

### Retrieving Referral Data:
To get all user referrals user `referrer` relationship property along with user object.
```php
foreach ($user->referrals as $referral)
{
$referral->referral_token;
}
```

<hr>

To get user referral details from the referrals table use the property `referrals`.
```php
// Get authenticated referral token
auth()->user()->referral->referral_token;
```

<hr>

To check if the user is invited by another user, use the `isReferred()` method on the user object.
```php
protected $fillable = ['referral_token', 'referrer_id'];
// Return true if the user is invited
// and false if the user was not invited by other user.
$user->isReferred();
```

Add `referral_link` to `protected $append` to use it as user property:
<hr>

To check of the user has referral token or not use the `hasReferralToken()` method.
```php
// Return true if the user has referral token, or false if not.
$user->hasReferralToken();
```
<hr>

To generate user referral token, use the `generateReferalToken()` method. The method
checks if the user has referral token or not, then it generated the referrals token.
```php
$user->generateReferralToken();
```
<hr>

To get the referral link you can either add `referral_link` attribute to the `$append` array inside
`User` model and use it as user attribute,
```php
protected $append = ['referral_link'];
```
Or, you can use the `getReferralLink()` method with the user object.

```php
// Get the referral link with the user redirect route
// ex: https://mydomain.com/register?ref=34532234
$user->getReferralLink();
```

<hr>

To get user referral token you can either add `referral_token` attribute to the `$append` array
inside the `User` model and use it as attribute,
```php
protected $append = ['referral_token'];
```
Or, you can use the `getReferralToken()` method with the user object.
```php
$user->getReferralToken();
```

## Register with Nova
Add nova resource `Referral` will be automatically published during publishing the service provider, to use
Expand Down
4 changes: 2 additions & 2 deletions src/app/Listeners/UpdateReferralsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public function __construct() { }
*/
public function handle($event): void
{
$event->user->referrals()->create(['referrer_id' => $event->user->getReferrerIdFromReferralToken()]);
$event->user->referral()->create(['referrer_id' => $event->user->getReferrerIdFromReferralToken()]);
}
}
}
2 changes: 1 addition & 1 deletion src/app/Models/Referral.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function referrer(): BelongsTo
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function allReferrals()
public function referrals()
{
return $this->hasMany(Referral::class, 'referrer_id', 'user_id');
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/Nova/Referral.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function fields(NovaRequest $request)
}),
Number::make('# Referrals', 'referrals_count')->sortable(),
BelongsTo::make('Referrer', 'referrer', User::class)->display('first_name'),
HasMany::make('Referrals', 'allReferrals', Referral::class)
HasMany::make('Referrals', 'referrals', Referral::class)
];
}

Expand Down Expand Up @@ -105,6 +105,6 @@ public function actions(NovaRequest $request)

public static function indexQuery(NovaRequest $request, $query)
{
$query->withCount('allReferrals as referrals_count');
$query->whereNotNull('referral_token')->withCount('referrals as referrals_count');
}
}
36 changes: 29 additions & 7 deletions src/app/Traits/Referrerable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,42 @@
use Asciisd\ReferralsLaravel\app\Models\Referral;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Ramsey\Uuid\Uuid;

trait Referrerable
{

/**
* Get referrals.
*
* @return HasMany
*/
public function referrals(): HasMany
{
return $this->hasMany(Referral::class, 'referrer_id', 'id');
}


/**
* Get referral detials from referrals table.
*
* @return HasMany
*/
public function referral(): HasOne
{
return $this->hasOne(Referral::class, 'user_id', 'id');
}


/**
* Check if user is registered with referral token or not.
*
* @return bool
*/
public function isReferred(): bool
{
return isset($this->referrable);
return isset($this->referral->referrer_id);
}


Expand All @@ -35,7 +52,7 @@ public function isReferred(): bool
*/
public function hasReferralToken(): bool
{
return isset($this->referrals()->first()->referral_token);
return isset($this->referral->referral_token);
}

/**
Expand All @@ -46,7 +63,7 @@ public function hasReferralToken(): bool
public function generateReferralToken(): bool
{
if (!$this->hasReferralToken()) {
return $this->referrals()->first()->update(['referral_token' => random_int(1000000, 9999999)]);
return $this->referral()->update(['referral_token' => random_int(1000000, 9999999)]);

}

Expand All @@ -60,7 +77,7 @@ public function generateReferralToken(): bool
*/
public function getReferralLink()
{
return route(config('referrals.referral_route'), ['ref' => $this->referrals()->first()->referral_token]);
return route(config('referrals.referral_route'), ['ref' => $this->referral->referral_token]);
}


Expand All @@ -84,7 +101,7 @@ public function referralLink(): Attribute
*/
public function getReferralToken()
{
return $this->referrals()->first()->referral_token;
return $this->referral->referral_token;
}


Expand All @@ -109,14 +126,19 @@ public function referralToken(): Attribute
*/
public function referrerId()
{
return $this->id;
return $this->id; //TODO::Fix with nova to show data with choosen referrerId
}

/**
* Get the referralId from the ref cookie by referral token.
*
* @return null
*/
public function getReferrerIdFromReferralToken()
{
// Get referral token from query.
$referral_token = request()->hasCookie('referral_token') ? request()->cookie('referral_token') : null;
return $referral_token != null ? Referral::where('referral_token', $referral_token)->first()->referrer->id : null;
return $referral_token != null ? Referral::where('referral_token', $referral_token)->first()->referrer->referrerId() : null;
}

}

0 comments on commit f5e1418

Please sign in to comment.