Weave 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
Laravel Extended Relationships

Laravel Extended Relationships Laravel Package

mr-punyapal/laravel-extended-relationships

Extra Eloquent relationship helpers for Laravel that cut database queries, improve performance, and reduce duplicate code. Requires PHP 8.3+ and Laravel 12/13, with easy Composer installation.

View on GitHub
Deep Wiki
Context7

laravel-extended-relationships adds efficient custom relationship helpers to Laravel Eloquent models. Drop the HasExtendedRelationships trait into a model to access relationships that cut database queries, boost performance, and eliminate repetitive relationship code. Built for modern stacks with support for PHP 8.3+ and Laravel 12/13, plus a bundled Laravel Boost skill for on-demand guidance.

  • Extra Eloquent relationships that reduce query counts
  • Simple trait-based opt-in per model
  • Less duplicate code across relations
  • Compatible with Laravel 12 and 13
  • Ships a Laravel Boost skill for AI-assisted development
Frequently asked questions about Laravel Extended Relationships
How does `belongsToManyKeys` reduce database queries compared to standard Eloquent relationships?
`belongsToManyKeys` consolidates multiple `belongsTo` relationships (e.g., `created_by`, `updated_by`) into a single query using a pivot-like structure, avoiding N+1 queries. For example, fetching 3 auditors in one query instead of 3 separate ones. Benchmark with `DB::enableQueryLog()` to compare before/after.
Can I use this package with Laravel 10 or PHP 8.1? The README says it requires Laravel 11+.
No, the package dropped support for Laravel 10/PHP 8.1 in v2.0.0. If you’re on Laravel 10, either downgrade to v1.x or plan a migration to Laravel 11+ to use the latest features. Check the [changelog](https://github.com/MrPunyapal/laravel-extended-relationships/blob/main/CHANGELOG.md) for version-specific changes.
What’s the difference between `belongsToManyKeys` and `hasManyArrayColumn`? When should I use each?
`belongsToManyKeys` is for normalized relationships (e.g., audit logs with `user_id` and `action` columns in a pivot table), while `hasManyArrayColumn` works with legacy array/JSON columns (e.g., `metadata` in a single table). Use `belongsToManyKeys` for performance-critical, normalized data; `hasManyArrayColumn` for quick fixes on unstructured data.
Will this package work with Livewire or InertiaJS for real-time dashboards?
Yes, it’s ideal for Livewire/Inertia apps. For example, `belongsToManyKeys` can fetch all auditors (creator, updater, deleter) in one query, reducing payload size and improving dashboard responsiveness. Pair it with Laravel’s caching (e.g., `remember()`) for further optimization.
How do I debug or inspect the SQL queries generated by `belongsToManyKeys`?
Use Laravel’s built-in tools: `toSql()` on the relationship or enable query logging with `DB::enableQueryLog()`. For complex queries, install `laravel-debugbar` to visualize joins and subqueries. The package doesn’t obscure SQL—it just optimizes it.
Is there a performance tradeoff for using `belongsToManyKeys`? What about large datasets?
The tradeoff is increased query complexity (e.g., joins with subqueries), but it reduces total queries. Test with your dataset size—start with 10K+ records to ensure no lock contention in high-concurrency apps (e.g., SaaS). For polymorphic relationships, consider indexing pivot columns.
Can I use this package with Lumen or non-Laravel PHP projects?
It’s designed for Laravel 11–13+ and untested in Lumen due to missing Eloquent features. For non-Laravel PHP, this package won’t work—it relies on Laravel’s ORM and traits. Stick to raw SQL or Query Builder for those use cases.
How do I migrate from standard Eloquent relationships to `belongsToManyKeys`?
Start with a pilot: replace one repetitive relationship (e.g., `created_by`, `updated_by`) in a single model. Use `DB::enableQueryLog()` to compare query counts. Gradually roll out to other models, ensuring your pivot tables have the required columns (e.g., `model_id`, `related_id`, `action`).
Does this package support polymorphic relationships? How?
Yes, `belongsToManyKeys` supports polymorphic relationships by defining the `morphMap` in your model. For example, `belongsToManyKeys('auditors')->polymorphic()` will work with multiple related models (e.g., `User`, `Admin`) sharing the same pivot table. Ensure your pivot table uses `morph_type` and `morph_id` columns.
Are there alternatives to this package for optimizing Eloquent relationships?
Alternatives include Laravel Scout (for search-heavy relationships), custom accessors/mutators, or packages like `spatie/laravel-activitylog` for audit trails. However, this package uniquely targets query optimization for *existing* relationships without requiring new tables or external services. Evaluate based on your specific needs—e.g., if you need audit logs, `spatie/laravel-activitylog` might be better.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections