Download the PHP package edulazaro/larawards without Composer
On this page you can find all versions of the php package edulazaro/larawards. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download edulazaro/larawards
More information about edulazaro/larawards
Files in edulazaro/larawards
Package larawards
Short Description A rewards system for Laravel, allowing to create rewards like achievements or trophies.
License
Informations about the package larawards
Larawards
An way of implementing achievement on Laravel.
How to install Larawards
Execute this command on the Laravel root project folder:
Configure the database details on the .env
file and execute the migrations using:
Creating new awards
To create a new award (achievement, trophy, badge...) use this command, replacing FooAchievement
for the ward class name:
This will create a new award on the app/Awards/FooAchievement.php
.
An award can be of different types, like an achievement
or a badge
. To set the type, use thje $type
attribute:
An award can have one or more tiers. Each tier si something which can be awarded, like an achievement. Tiers allow to define similar achievements on a single file:
Each tier has a title
, which is the achievement/badge name and also a score
,` which is the score needed to reward the users with the achievement/badge.
Finally, the score
method is the place to code the logic which retrieves the current award score for the awardablep
user:
Registering awards
If you ant an model to accept rewards, you will need to use the HasWards
trait into it, like on this example:
The achievements should be registered with the User model or another model including the HasRewards
trait using the AwardProvider
:
Or grouped:
Awards can be registed to any model which uses the HasRewards
trait.
Checking rewards
When you need to check an award, you can do this:
As you can see, the award needs to be scoped first to the user This will add the reward if it matches the requirement of any tier. awardables.
You can also perform queries to check many awards at a time. This will check all awards assigned to a user:
You can also select a specific group:
Or a specific type:
Once an award is awarded, it will be added into the rewards
table.
Reward events
If you want, you can specify an event to be fired using the $event
attribute:
This event will be fired each time a user gets a reward.
Checking awards
To get the rewards for a user you can do:
To check if a specific award and tier has been rewarded:
You can query the rewards for many users:
Awards morph map
This works in the same way as the standard Laravel morph maps.
When a reward is inserte dinto the database, the award id will be the class where the award is defined. However this looks ugly. To fix it, use the Award::enforceMap
method on any service provider:
This will assign the specified alias to the desided Award classes. For example, the App\Awards\CommentsAchievement
will be represented as comments_achievement
which is more readable.
License
The Laravel framework is open-sourced software licensed under the MIT license.