Skip to content

Aecy/laravel-badge

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel - Badge System

This package helps you for create a badge, you can add a badge to an user and more...

Features

You can reward your users when they post comments on your application, you can add new badge for comments and create your badges, for example create the premium badge, ...

Installation

Required

  • PHP 7.0 +
  • Laravel 5.5 +
  • Simple Comment system with column user_id

You can install the package using composer and run vendor:publish

$ composer require aecy/badge
$ php artisan vendor:publish --provider='Aecy\Badge\BadgeServiceProvider' --tag="migrations"

The badge for your Comment model is already create and good for using in your application, for using it on Comment you need to add this trait in your model.

use Badgeable;

DON'T FORGET : IMPORT THE CLASS.

Create Badge

Create your own badge, in App\Events\ create Premium.php if the folder Events doenst exist in App folder, create him.

In the Premium.php copy and paste :

<?php

namespace App\Events;

use App\User;

class Premium
{
    public $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

Why i've using the User model in this events class, its because my Premium System its apply to an User.

Extends a class of BadgeSubcriber.php and add an Events Listener like this :

public function subscribe ($events)
{
    parent::boot();
    $events->listen('App\Events\Premium', [$this, 'onPremium']);
}

and now add this function in your class which extends of BadgeSubcriber

public function onPremium ($event)
{
    $badge = $this->badge->unlockActionFor($event->user, 'premium');
    $this->notifyBadge($event->user, $badge);
}

You're done !

PHPUnit Tests

And if you want, you can create a phpunit test for check the unlock of your own badge like this :

public function test_unlock_premium_badge()
{
    Badge::create(['name' => 'Premium', 'action' => 'premium', 'action_count' => 0]);
    $user = factory(User::class)->create();
    event(new Premium($user));
    $this->assertEquals(1, $user->badges()->count());
}

License

MIT

Releases

No releases published

Packages

No packages published

Languages