Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Tailwind Merge Php

Tailwind Merge Php Laravel Package

gehrisandro/tailwind-merge-php

Merge Tailwind CSS class strings in PHP with automatic conflict resolution (last class wins), ported from tailwind-merge. Supports Tailwind v3.0–v3.4, configurable and cacheable. Requires PHP 8.1+.

View on GitHub
Deep Wiki
Context7

Automatically resolves Tailwind CSS class conflicts in PHP

Frequently asked questions about Tailwind Merge Php
How do I install TailwindMerge for PHP in a Laravel project?
Run `composer require gehrisandro/tailwind-merge-php` to install the package. No additional Laravel-specific setup is required, though you can bind it to the service container for dependency injection. The package works standalone or alongside the Laravel wrapper.
Does this package support Tailwind v3.4? What if I’m using an older version?
Yes, it supports Tailwind v3.0–3.4 out of the box. For older versions (e.g., v2.x), you’ll need to manually configure `classGroups` to match your Tailwind setup, as the package is tailored for v3.x. Check the [original tailwind-merge config guide](https://github.com/dcastil/tailwind-merge) for reference.
Can I use this for dynamic class merging in Livewire or Alpine.js components?
Absolutely. The package resolves conflicts server-side, so you can merge classes dynamically in Livewire methods or Alpine.js reactive properties without client-side JavaScript. Example: Return merged classes from a Livewire method to update UI seamlessly.
How does conflict resolution work? For example, merging `bg-red-500` and `bg-blue-500`.
The package follows Tailwind’s logic: later classes overwrite earlier ones. In your example, `bg-blue-500` wins, and `bg-red-500` is removed. This matches the behavior of the JavaScript `tailwind-merge` library, ensuring consistency between frontend and backend.
Is there a performance impact if I merge classes in loops (e.g., for 100+ items)?
For most cases, the impact is negligible. However, you can enable PSR-16 caching (e.g., Redis or file cache) via `TailwindMerge::factory()->withCache($cache)` to optimize repeated merges. Test with your workload to confirm.
How do I configure custom Tailwind setups, like arbitrary values or non-standard variants?
Use `TailwindMerge::factory()->withConfiguration(['classGroups' => [...]])` to extend the default configuration. Refer to the [original library’s config guide](https://github.com/dcastil/tailwind-merge#configuration) for schema details. For arbitrary values, ensure they’re included in `classGroups`.
Can I integrate this with Laravel Blade templates or view composers?
Yes. Bind the instance to Laravel’s container (e.g., in a service provider) and inject it into Blade views or view composers. Example: `{{ $tw->merge($dynamicClasses) }}` in Blade. This enables server-side merging for dynamic templates.
What’s the difference between `TailwindMerge::instance()` and `TailwindMerge::factory()`?
`instance()` returns a stateless, default-configured instance for quick use. `factory()` lets you customize the instance (e.g., with caching or config) before making it. Use `factory()` for reusable, configured instances in your app’s lifecycle.
Does this package work with Laravel’s email or PDF generation (e.g., Spatie Mail, DomPDF)?
Yes. Merge classes dynamically in email templates or PDF generators (e.g., `Mail::send()` or DomPDF) to resolve conflicts server-side. Example: `$message->withTailwindClasses($tw->merge('p-4', 'dark:p-8'))`.
Are there alternatives to this package? When would I choose another?
Alternatives include client-side JavaScript solutions (e.g., `tailwind-merge` npm package) or custom PHP logic. Choose this package if you need server-side merging (e.g., for SEO, PDFs, or performance-critical paths) or Laravel integration. For frontend-only use, the JS version may suffice.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai