PHP code example of maize-tech / laravel-badges

1. Go to this page and download the library: Download maize-tech/laravel-badges 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/ */

    

maize-tech / laravel-badges example snippets


return [

    /*
    |--------------------------------------------------------------------------
    | Badge model
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the badge model.
    |
    */

    'model' => Maize\Badges\Models\BadgeModel::class,

    /*
    |--------------------------------------------------------------------------
    | Badges
    |--------------------------------------------------------------------------
    |
    | Here you may specify the list of fully qualified class name of badges.
    |
    */

    'badges' => [
        // App\Badges\FirstLogin::class,
    ],

];



namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Maize\Badges\HasBadges;
use Maize\Badges\InteractsWithBadges;

class User extends Authenticatable implements HasBadges
{
    use InteractsWithBadges;
}



namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\Badge;

class FirstLogin extends Badge
{
    public static function isAwarded(Model $model): bool
    {
        return $model->logins()->exists();
    }
}

'badges' => [
    App\Badges\FirstLogin::class,
],



namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\ProgressableBadge;

class FiveLogins extends ProgressableBadge
{
    public static function getTotal(): int
    {
        return 5;
    }

    public static function getCurrent(Model $model): bool
    {
        return $model->logins()->count();
    }
}



namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\Badge;

class FirstLogin extends Badge
{
    public static function isAwarded(Model $model): bool
    {
        return $model->logins()->exists();
    }
    
    public static function metadata(): array
    {
        $slug = static::slug();

        return [
            'name' => __("badges.{$slug}.name"),
            'description' => __("badges.{$slug}.name"),
        ];    
    }
}

use App\Badges\FirstLogin;
use Maize\Badges\Models\BadgeModel;

$user->giveBadge(FirstLogin::class);

$user->badges()->first()->metadata; // returns the list of metadata attributes

$user->badges()->first()->getMetadata('description'); // returns the attribute with key 'description'

FirstLogin::metadata(); // returns the list of metadata attributes

FirstLogin::getMetadata('description'); // returns the metadata attribute with key 'description'



namespace App\Badges;

use Illuminate\Database\Eloquent\Model;
use Maize\Badges\Badge;

class FirstLogin extends Badge
{
    public static function slug(): string
    {
        return str(static::class)
            ->classBasename()
            ->kebab()
            ->toString(); // returns 'first-login'
    }

    public static function isAwarded(Model $model): bool
    {
        return $model->logins()->exists();
    }
}

use App\Badges\FirstLogin;

$user->giveBadge(FirstLogin::class);

FirstLogin::giveTo($user);

use App\Badges\FirstLogin;

$user->hasBadge(FirstLogin::class)

use Maize\Badges\Models\BadgeModel;

$user->badges->map->badge; // returns the list of awarded badges slug

$user->syncBadges();

use Maize\Badges\Commands\ClearBadgesCommand;

$schedule->command(ClearBadgesCommand::class)->daily();
bash
php artisan badges:install