定制 kalel1500/laravel-tailwind-merge 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kalel1500/laravel-tailwind-merge

Composer 安装命令:

composer require kalel1500/laravel-tailwind-merge

包简介

TailwindMerge for Laravel merges multiple Tailwind CSS classes by automatically resolving conflicts between them

README 文档

README

Note

This project is a fork from tailwind-merge by Sandro Gehri.

TailwindMerge for Laravel

GitHub Workflow Status (main) Total Downloads Latest Version License

TailwindMerge for Laravel allows you to merge multiple Tailwind CSS classes and automatically resolves conflicts between classes by removing classes conflicting with a class defined later. This is especially helpful when you want to override Tailwind CSS classes in your Blade components.

A Laravel / PHP port of tailwind-merge by dcastil.

Supports Tailwind v4.0 up to v4.2

If you are NOT using Laravel, you can use the TailwindMerge for PHP directly.

Table of Contents

Get Started

Requires Laravel 12

First, install TailwindMerge for Laravel via the Composer package manager:

composer require kalel1500/laravel-tailwind-merge

Optionally, publish the configuration file:

php artisan vendor:publish --provider="Thehouseofel\TailwindMerge\TailwindMergeServiceProvider"

This will create a config/tailwind-merge.php configuration file in your project, which you can modify to your needs using environment variables. For more information, see the Configuration section:

Finally, you may use TailwindMerge in various places like your Blade components:

// circle.blade.php
<div {{ $attributes->twMerge('w-10 h-10 rounded-full bg-red-500') }}></div>

// your-view.blade.php
<x-circle class="bg-blue-500" />

// output
<div class="w-10 h-10 rounded-full bg-blue-500"></div>

TailwindMerge is not only capable of resolving conflicts between basic Tailwind CSS classes, but also handles more complex scenarios:

use Thehouseofel\TailwindMerge\Facades\TailwindMerge;

// conflicting classes
TailwindMerge::merge('block inline'); // inline
TailwindMerge::merge('pl-4 px-6'); // px-6

// non-conflicting classes
TailwindMerge::merge('text-xl text-black'); // text-xl text-black

// with breakpoints
TailwindMerge::merge('h-10 lg:h-12 lg:h-20'); // h-10 lg:h-20

// dark mode
TailwindMerge::merge('text-black dark:text-white dark:text-gray-700'); // text-black dark:text-gray-700

// with hover, focus and other states
TailwindMerge::merge('hover:block hover:inline'); // hover:inline

// with the important modifier
TailwindMerge::merge('!font-medium !font-bold'); // !font-bold

// arbitrary values
TailwindMerge::merge('z-10 z-[999]'); // z-[999] 

// arbitrary variants
TailwindMerge::merge('[&>*]:underline [&>*]:line-through'); // [&>*]:line-through

// non tailwind classes
TailwindMerge::merge('non-tailwind-class block inline'); // non-tailwind-class inline 

It's possible to pass the classes as a string, an array or a combination of both:

TailwindMerge::merge('h-10 h-20'); // h-20
TailwindMerge::merge(['h-10', 'h-20']); // h-20
TailwindMerge::merge(['h-10', 'h-20'], 'h-30'); // h-30
TailwindMerge::merge(['h-10', 'h-20'], 'h-30', ['h-40']); // h-40

Usage

For in depth documentation and general PHP examples, take a look at the tales-from-a-dev/tailwind-merge-php repository.

Use in Laravel Blade Components

Create your Blade components as you normally would, but instead of specifying the class attribute directly, use the mergeClasses method:

// circle.blade.php
<div {{ $attributes->twMerge('w-10 h-10 rounded-full bg-red-500') }}></div>

Now you can use your Blade components and pass additional classes to merge:

// your-view.blade.php
<x-circle class="bg-blue-500" />

This will render the following HTML:

<div class="w-10 h-10 rounded-full bg-blue-500"></div>

Note: Usage of $attributes->merge(['class' => '...']) is currently not supported due to limitations in Laravel.

Merge classes on multiple elements

By default Laravel allows you to only merge classes in one place. But with TailwindMerge you can merge classes on multiple elements by using twMergeFor():

// button.blade.php
<button {{ $attributes->withoutTwMergeClasses()->twMerge('p-2 bg-gray-900 text-white') }}>
    <svg {{ $attributes->twMergeFor('icon', 'h-4 text-gray-500') }} viewBox="0 0 448 512"><path d="..."/></svg>
    
    {{ $slot }}
</button>

You can now specify additional classes for the button and the svg icon:

// your-view.blade.php
<x-button class="bg-blue-900" class:icon="text-blue-500">
  Click Me
</x-button>

This will render the following HTML:

<button class="p-2 blue text-white">
  <svg class="h-4 text-blue-500" viewBox="0 0 448 512"><path d="..."/></svg>

  Click Me
</button>

Note: Use withoutTwMergeClasses() on your main attributes bag, otherwise all class:xyz attributes will be rendered in the output.

Use Laravel Blade Directive

The package registers a Blade directive which can be used to merge classes in your Blade views:

@twMerge('w-10 h-10 rounded-full bg-red-500 bg-blue-500') // w-10 h-10 rounded-full bg-blue-500

// or multiple arguments
@twMerge('w-10 h-10 rounded-full bg-red-500', 'bg-blue-500') // w-10 h-10 rounded-full bg-blue-500

Everywhere else in Laravel

If you don't use Laravel Blade, you can still use TailwindMerge by using the Facade or the helper method directly:

Facade

use Thehouseofel\TailwindMerge\Facades\TailwindMerge;

TailwindMerge::merge('w-10 h-10 rounded-full bg-red-500 bg-blue-500'); // w-10 h-10 rounded-full bg-blue-500

Helper Method

twMerge('w-10 h-10 rounded-full bg-red-500 bg-blue-500'); // w-10 h-10 rounded-full bg-blue-500

More usage examples

Take a look at the TailwindMerge for PHP repository.

Configuration

This package provides two types of configuration:

  • merge_config: Controls how Tailwind classes are merged
  • cache: Controls Laravel-specific caching behavior

Modify merge process

If you are using Tailwind CSS without any extra config, you can use TailwindMerge right away. And stop reading here.

If you're using a custom Tailwind config, you may need to configure TailwindMerge as well to merge classes properly.

By default, TailwindMerge is configured in a way that you can still use it if all the following apply to your Tailwind config:

  • Only using color names which don't clash with other Tailwind class names
  • Only deviating by number values from number-based Tailwind classes
  • Only using font-family classes which don't clash with default font-weight classes
  • Sticking to default Tailwind config for everything else

If some of these points don't apply to you, you need to customize the configuration.

All Tailwind Merge-related configuration must now be defined inside the merge_config key in your config file.

If TailwindMerge is not able to merge your classes properly, you can modify the merge process by customizing existing class groups or adding new ones.

For example, if you want to add a custom font size of very-large:

// config/tailwind-merge.php

return [
    'merge_config' => [
        'classGroups' => [
            'font-size' => [
                ['text' => ['very-large']],
            ],
        ],
    ],
];

For a more detailed explanation of the configuration options, visit the original package documentation.

Cache configuration

You can configure caching behavior using the cache key:

// config/tailwind-merge.php

return [
    'cache' => [
        'enabled' => env('TW_MERGE_CACHE_ENABLED', true),
        'store' => env('TW_MERGE_CACHE_STORE', 'file'), // or null to use default store
    ],
];

Or using environment variables:

TW_MERGE_CACHE_ENABLED=
TW_MERGE_CACHE_STORE=
  • enabled: Enable or disable caching entirely.
  • store: The cache store to use. If null, the default store is used.

Note

By default, this package uses the file cache store instead of Laravel's default.

This is intentional, as Tailwind Merge generates a high number of small cache entries. Using the database driver may lead to performance issues and excessive database queries.

How it works internally

When using the file store, the package automatically maps it to a dedicated cache store named file_tw_merge.

This store uses a separate directory:

storage/framework/cache/data/tw-merge

This ensures:

  • Tailwind Merge cache entries are isolated from your application's main cache
  • Your default cache store remains clean and easier to debug
  • Better performance compared to database-based caching

You can still override this behavior by explicitly setting another store or using null to fallback to Laravel's default cache store.

Custom cache store

If you want full control, you can define your own cache store in config/cache.php and reference it here.

Laravel TailwindMerge is an open-sourced software licensed under the MPL-2.0.

kalel1500/laravel-tailwind-merge 适用场景与选型建议

kalel1500/laravel-tailwind-merge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「classes」 「laravel」 「merge」 「tailwindcss」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kalel1500/laravel-tailwind-merge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kalel1500/laravel-tailwind-merge 我们能提供哪些服务?
定制开发 / 二次开发

基于 kalel1500/laravel-tailwind-merge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MPL-2.0
  • 更新时间: 2026-03-17