PHP code example of edulazaro / larawards

1. Go to this page and download the library: Download edulazaro/larawards library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

edulazaro / larawards example snippets


php artisan migrate

php artisan make:award FooAchievement

/** @var string The award type. */
public $type = 'achievement';

/** @var protected The award tiers. */
protected array $tiers = [
    'comment_written' => [
        'score' => 1,
        'title' => 'First Comment Written',
    ],
    '3_comments_written' => [
        'score' => 3,
        'title' => '3 Comments Written',
    ],
    '5_comments_written' => [
        'score' => 5,
        'title' => '5 Comments Written',
    ],
    '10_comments_written' => [
        'score' => 10,
        'title' => '10 Comments Written',
    ],
    '20_comments_written' => [
        'score' => 20,
        'title' => '20 Comments Written',
    ],
];

/**
* Get the awardable score a user
 *
* @return int
*/
public function score(): int
{
    return $this->rewardable->comments()->count();
}

namespace App\Models;

use EduLazaro\Larawards\Concerns\HasRewards;

class User
{
    use HasRewards;

}

User::awardable(AchievementsBadge::class);

User::awardableGroup('achievements', [
    CommentsAchievement::class,
    LessonsWatchedAchievement::class
]);

FooAchievement::scope($user)->check();

User::awardables()->check();

User::awardables()->group('top_awards')->check();

User::awardables()->where('type', 'achievement')->check();

namespace App\Awards;

use EduLazaro\Larawards\Concerns\IsAward;
use EduLazaro\Larawards\Contracts\AwardInterface;
use App\Events\AchievementUnlocked;

class CommentsAchievement implements AwardInterface
{
    use IsAward;

    // ...

    protected string $event = AchievementUnlocked::class;
    // ...
}

$rewards = $user->rewards;

$isRewarded = $user->rewards()->where('name', 'comment_written')->exists();

use EduLazaro\Larawards\Models\Reward;

// ...
Reward::where('award_id', 'comments_achievement')->get();

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use EduLazaro\Larawards\Collections\Awards;

use App\Awards\CommentsAchievement;
use App\Awards\LikesAchievement;

class AwardServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Awards::enforceMap([
            'comments_achievement' => CommentsAchievement::class,
            'likes_achievement' => LikesAchievement::class,
        ]);
    }
}