2017 © Pedro Peláez
 

laravel credits

Laravel Package for easily adding User Credits support

image

iamjaime/credits

Laravel Package for easily adding User Credits support

  • Tuesday, July 17, 2018
  • by codehead
  • Repository
  • 1 Watchers
  • 0 Stars
  • 15 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel User Credits Package

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',
      ];
  }
If you are using Laravel Spark you will need to do the following :

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'
        ],

If you are using Team Billing from Laravel Spark and would like to use credits per team do the following :

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;

    //
}

Now you are all setup and ready to go!

Some examples Below :

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);

The Versions

17/07 2018

dev-master

9999999-dev

Laravel Package for easily adding User Credits support

  Sources   Download

MIT

by Jaime Bernal

17/07 2018

dev-develop

dev-develop

Laravel Package for easily adding User Credits support

  Sources   Download

MIT

by Jaime Bernal