Code Monkey home page Code Monkey logo

laravel-badge's Introduction

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

laravel-badge's People

Contributors

aecy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.