PHPackages                             protich/auto-join-eloquent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. protich/auto-join-eloquent

ActiveLibrary[Database &amp; ORM](/categories/database)

protich/auto-join-eloquent
==========================

Extend Eloquent to support auto-joining relationships and aliasing for SELECT, WHERE, ORDERBY, GROUPBY and HAVING clauses

0.9.9(1mo ago)497↓50%4[3 issues](https://github.com/protich/auto-join-eloquent/issues)MITPHPPHP &gt;=8.2

Since Apr 2Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/protich/auto-join-eloquent)[ Packagist](https://packagist.org/packages/protich/auto-join-eloquent)[ RSS](/packages/protich-auto-join-eloquent/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (20)Used By (0)

Auto Join Eloquent
==================

[](#auto-join-eloquent)

**auto-join-eloquent** is a Laravel package that automates the process of joining related Eloquent models in your queries. By leveraging relationship metadata and centralized alias management, the package simplifies complex query building and ensures consistent `JOIN` clauses throughout your application.

This package was inspired by the need for dynamic queries in osTicket’s custom forms and fields. It was developed to address the challenges of handling dynamic and nested auto `JOIN` operations in such environments.

Features
--------

[](#features)

- **Automatic Join Processing**: Automatically joins related models based on defined Eloquent relationships without requiring manual `JOIN` clauses.
- **Nested Relationships**: Supports deep nesting of relationships using dot (`.`) or double-underscore (`__`) notation.
- **Aggregate Functions**: Seamlessly compile aggregates in `SELECT` and `HAVING` clauses with built-in support for functions such as `COUNT`, `SUM`, `AVG`, `MIN`, and `MAX`.

### Aliasing Management

[](#aliasing-management)

- **Custom Aliases**: Define custom join aliases directly in your models via a `$joinAliases` property.
- **Auto Generated Aliasing**: Optionally, the package can automatically generate simple sequential aliases (`A`, `B`, `C`, …, then `A1`, `B1`, etc.) or descriptive aliases based on relationship keys.
- **Raw SQL Handling**: Supports raw `HAVING` clauses and can intelligently compile aggregate expressions when raw SQL references relationships.

### Clause-Specific Compilation

[](#clause-specific-compilation)

- **SelectCompiler**: Processes `SELECT` clause columns (adds aliases and handles aggregates).
- **WhereCompiler**: Transforms `WHERE` clause columns into fully qualified column names (without aliasing).
- **HavingCompiler**: Compiles `HAVING` clause expressions (handles aggregates without aliasing).
- **OrderByCompiler**: Compiles `ORDER BY` clause expressions, ensuring that the sorting columns are properly resolved.
- **GroupByCompiler**: Compiles `GROUP BY` clause expressions, guaranteeing that grouping columns are correctly qualified with their respective aliases.

Installation
------------

[](#installation)

Install via Composer:

```
composer require protich/auto-join-eloquent
```

Usage
-----

[](#usage)

### Enabling Auto Join

[](#enabling-auto-join)

Include the `AutoJoinTrait` in your Eloquent models to enable auto-join functionality. For example, in your `User` model:

```
