spatie/emoji
Work with emoji in PHP without relying on your IDE/font. Use the Spatie\Emoji\Emoji class to access emoji as constants or friendly camelCase methods like Emoji::grinningFace(), or fetch all emojis via Emoji::all().
Installation:
composer require spatie/emoji
Add the service provider to config/app.php (Laravel auto-discovers it in Laravel 5.5+):
Spatie\Emoji\EmojiServiceProvider::class,
First Use Case: Display an emoji in a Blade view:
// In a controller
return view('welcome', ['emoji' => Emoji::grinningFace()]);
// In Blade
<p>{{ $emoji }}</p> <!-- Renders π -->
Quick Reference:
Emoji::grinningFace() β πEmoji::countryFlag('us') β πΊπΈEmoji::all() β Returns an associative array of all emojis.app/Providers/EmojiServiceProvider.php: If extending functionality (e.g., custom emoji sets).config/emoji.php: For package-specific configurations (if any).// Render a single emoji
@emoji('grinningFace')
// Render a list of emojis (e.g., from a database)
@emojiList($user->favoriteEmojis)
$emoji = Emoji::fromShortcode(':thumbsup:'); // Returns π
use Spatie\Emoji\Rules\Emoji;
public function rules()
{
return [
'comment' => ['required', new Emoji],
];
}
$cleanText = preg_replace('/[^\P{Emoji}]/u', '', $userInput);
$countryFlags = collect(['us', 'jp', 'be'])
->map(fn ($code) => Emoji::countryFlag($code));
// Returns: ['πΊπΈ', 'π―π΅', 'π§πͺ']
:smile:)function parseEmojiShortcodes(string $text): string
{
return preg_replace_callback(
'/:([a-z0-9_]+):/i',
fn ($matches) => Emoji::{$matches[1]}() ?? $matches[0],
$text
);
}
parseEmojiShortcodes('I :grinningFace: today!'); // "I π today!"
use Spatie\Emoji\Casts\Emoji;
class Post extends Model
{
protected $casts = [
'emoji_reaction' => Emoji::class,
];
}
Post::where('emoji_reaction', Emoji::grinningFace())->get();
Emoji::all() to populate a dynamic emoji picker component.return response()->json(['message' => 'Hello! ' . Emoji::wave()]);
spatie/laravel-markdown to support emoji shortcodes in Markdown.Method Naming Quirks:
100Points) are supported but may cause IDE autocompletion issues. Use the CHARACTER_* constants for reliability:
Emoji::CHARACTER_100_POINTS; // Instead of Emoji::100Points()
Unicode Normalization:
mb_convert_encoding() to normalize Unicode:
$normalizedEmoji = mb_convert_encoding(Emoji::grinningFace(), 'UTF-8', 'UTF-8');
Flag Emoji Edge Cases:
'bl' for Saint BarthΓ©lemy) may not render correctly due to Unicode limitations. Verify flags with Emoji::countryFlag('bl') before production use.Performance with Emoji::all():
$emojis = Cache::remember('all-emojis', now()->addDays(7), fn () => Emoji::all());
IDE Autocompletion:
Emoji::π()). Stick to camelCase methods or constants for reliability.Emoji Not Rendering?:
var_dump(bin2hex(Emoji::grinningFace()));
Method Not Found:
Validation Failures:
Emoji rule validates against the packageβs emoji set. For custom emojis, extend the rule:
use Spatie\Emoji\Rules\Emoji as BaseEmoji;
class CustomEmoji extends BaseEmoji
{
protected function getAllowedEmojis(): array
{
return array_merge(parent::getAllowedEmojis(), ['π¦']);
}
}
Custom Emoji Sets:
Emoji class to add custom emojis:
class CustomEmoji extends \Spatie\Emoji\Emoji
{
public static function unicorn(): string
{
return 'π¦';
}
}
EmojiServiceProvider.php:
$this->app->bind(\Spatie\Emoji\Emoji::class, CustomEmoji::class);
Shortcode Aliases:
:unicorn:):
// In a service provider
Emoji::macro('unicorn', fn () => 'π¦');
Blade Directives:
@emoji directive in EmojiServiceProvider.php:
Blade::directive('emoji', function ($expression) {
return "<?php echo \\Spatie\\Emoji\\Emoji::{$expression}(); ?>";
});
Emoji Skin Tones:
π¨π½). Access them via:
Emoji::manLightSkinTone(); // π¨π½
Emoji::manMediumLightSkinTone(); // π¨πΌ
Emoji::π()).Emoji::all() to preload emojis for bulk operations (e.g., migrating legacy shortcodes to emojis):
$posts = Post::all();
$posts->each(function ($post) {
$post->update(['content' => str_replace(':smile:', Emoji::
How can I help you explore Laravel packages today?