kbzone's avatar

Best way to implement a «Badge» system

I'm searching for recommendations about the best way to implement a «Badge» system for every user of my app, and specially how to show it in Blade. I guess that the idea is simple, but with my short experience in Laravel does not allow me to see a better solution.

I was thinking to create an UserBadgesService with a static method Badge that receive a User model instance and a code of what type of badge i want to show, something like:

public static function badge(User $user, $badge_type)

in blade, i guess that i could implemented like this:

@if (\App\Services\UserBadgesService::badge($user, 'sales') == 'low')
<x-badge type="warning" text="Low sales" />
@endif

@if (\App\Services\UserBadgesService::badge($user, 'profile_updated') == true)
<x-badge type="success" text="Profile is updated" />
@endif

But I'm sure that this approach is awful: injecting a Service Class directly in Blade, or even checking for every type of Badge one by one is ridiculous.

So I'm here asking for betters implementations of this. Just a detail to keep in mind, every type of Badge that I will check will have their more or less complexity.

I will appreciate every single comment and advice. Thanks community!

0 likes
2 replies

Please or to participate in this conversation.