- How do I install jfcherng/php-diff in a Laravel project?
- Run `composer require jfcherng/php-diff` in your project root. Ensure your server has PHP 8.3+ and the `ext-iconv` extension enabled, as required by the package.
- Which Laravel versions does this package support?
- The package supports Laravel 10.x+ since it requires PHP 8.3+. It has no Laravel-specific dependencies, so it works with any Laravel version meeting the PHP requirement.
- Can I use this for generating HTML diffs in a Laravel Blade template?
- Yes. Use the `SideBySide`, `Combined`, or `Inline` HTML renderers and include the default CSS via `DiffHelper::getStyleSheet()`. Embed the diff in Blade with `@php` directives or create a custom Blade component.
- What are the best renderers for API responses vs. admin panels?
- For APIs, use `JsonText` or `JsonHtml` for machine-readable diffs. For admin panels, `SideBySide` or `Combined` HTML renderers provide the most readable visual diffs for humans.
- How do I handle large file diffs (e.g., 100KB+) without timeouts?
- Set a `lengthLimit` in the `DifferOptions` to cap input size. For very large files, pre-process them (e.g., chunking) or use async processing with Laravel queues to avoid blocking requests.
- Is there a way to integrate this with Laravel’s service container?
- Yes. Bind the `Differ` class to the container manually, like `$this->app->bind(Differ::class, fn() => new Differ([], [], new DifferOptions()));`. For cleaner syntax, create a facade or use Laravel’s `make:command` for CLI tools.
- Does this package support custom CSS for HTML diffs?
- Yes. You can modify the default `diff-table.css` or create your own. The package provides `DiffHelper::getStyleSheet()` for the default CSS, but you can override it entirely for custom styling.
- What alternatives exist if I need more active maintenance?
- Consider `sebastianbergmann/diff` for a simpler, actively maintained alternative. For advanced diff algorithms, `google/diff-match-patch` (Google’s library) offers more features but requires manual integration.
- How do I store diffs in a PostgreSQL database?
- Use the `JsonText` or `JsonHtml` renderers to serialize diffs into JSON. Store them in a `jsonb` column for efficient querying and retrieval. For large HTML diffs, consider binary storage (e.g., S3).
- Can I use this for real-time collaborative editing (e.g., like Google Docs)?
- The package supports character-level diffs, which are useful for collaborative editing. However, for real-time sync, you’ll need additional logic (e.g., operational transformation) and may want to pair it with a WebSocket library like Laravel Echo.