Laravel Package for easily adding User Credits support
The purpose of this package is for you to be able to easily apply a credit based system to your existing Laravel Application., (*1)
For example : if you want to create a site that people must purchase credits in order to purchase items on the site, this package will make it easy for you to do., (*2)
To get started follow these steps:, (*3)
Install the package using composer, (*4)
composer require iamjaime/credits --dev
, (*5)
Then go to your config/app directory and add the following to the providers array:, (*6)
Iamjaime\Credits\UserCreditServiceProvider::class
, (*7)
Now you must run the migrations...., (*8)
php artisan migrate
, (*9)
Now go to your User's Model and add the following line at the top:, (*10)
use Iamjaime\Credits\Traits\UsesCredits;
, (*11)
then add the following line inside your class :, (*12)
use UsesCredits;
, (*13)
Your Model should now look something like this :, (*14)
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; use Iamjaime\Credits\Traits\UsesCredits; class User extends Authenticatable { use Notifiable, UsesCredits; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; }
Go to App/Providers/EventServiceProvider.php and add the following to the protected $listen
array:, (*15)
'Laravel\Spark\Events\Auth\UserRegistered' => [ 'Iamjaime\Credits\Listeners\UserRegistered' ],
Go to App/Providers/EventServiceProvider.php and add the following to the protected $listen
array:, (*16)
'Laravel\Spark\Events\Teams\TeamCreated' => [ 'Laravel\Spark\Listeners\Teams\UpdateOwnerSubscriptionQuantity', 'Iamjaime\Credits\Listeners\TeamCreated' ],
Go to your Team's Model and add the following line at the top:, (*17)
use Iamjaime\Credits\Traits\UsesTeamCredits;
, (*18)
then add the following line inside your class :, (*19)
use UsesTeamCredits;
, (*20)
Your Model should now look something like this :, (*21)
<?php namespace App; use Laravel\Spark\Team as SparkTeam; use Iamjaime\Credits\Traits\UsesTeamCredits; class Team extends SparkTeam { use UsesTeamCredits; // }
The following snippet will return the user object with the user's credits., (*22)
$user = App\User::where('id', '=', 1)->with('credit')->first(); //These are the user's credits.... $credits = $user->credit->amount;
The following snippet will return the team object with the team's credits., (*23)
$team = App\Team::where('id', '=', 1)->with('credit')->first(); //These are the team's credits.... $credits = $team->credit->amount;
Here is an example of how to update the user's credits., (*24)
$user = App\User::where('id', '=', 1)->first(); $user->updateCredits(500);
Here is an example of how to update the team's credits., (*25)
$team = App\Team::where('id', '=', 1)->first(); $team->updateCredits(500);