PHPackages                             ahfa92/laravel-relation-joins - 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. ahfa92/laravel-relation-joins

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

ahfa92/laravel-relation-joins
=============================

Adds the ability to join on a relationship by name.

1.0.10(5y ago)04MITPHPPHP ^7.1.3

Since Aug 10Pushed 4y agoCompare

[ Source](https://github.com/ahfa92/laravel-relation-joins)[ Packagist](https://packagist.org/packages/ahfa92/laravel-relation-joins)[ RSS](/packages/ahfa92-laravel-relation-joins/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (5)Versions (13)Used By (0)

Laravel Relation Joins
======================

[](#laravel-relation-joins)

[![Latest Stable Version](https://camo.githubusercontent.com/1d9f2400e19beebef4f6cf6fb5d7e2cc0034dc7da28174f3db2807e9910d7343/68747470733a2f2f706f7365722e707567782e6f72672f72656564776172652f6c61726176656c2d72656c6174696f6e2d6a6f696e732f762f737461626c65)](https://packagist.org/packages/reedware/laravel-relation-joins)[![Total Downloads](https://camo.githubusercontent.com/e105589f34365e3f3bcd7a23360c0d0b96b272dc0aa90c9671feb29503d8503b/68747470733a2f2f706f7365722e707567782e6f72672f72656564776172652f6c61726176656c2d72656c6174696f6e2d6a6f696e732f646f776e6c6f616473)](https://packagist.org/packages/reedware/laravel-relation-joins)[![Laravel Version](https://camo.githubusercontent.com/a6661ec47a8b380436113a110f5481b771ba13986be1d30368ef21d49154241f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d352e352532422d626c7565)](https://laravel.com/)[![Laravel Version](https://camo.githubusercontent.com/6d6922e88355d2aa1764eeb86be2a73e4626348bae59ea98b877bd22699e041c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d362e782532422d626c7565)](https://laravel.com/)[![Build Status](https://camo.githubusercontent.com/3ad35da33d093479f0f7ccf3afbb22e1d2d15ee949b0c7e4f6171161af6cf743/68747470733a2f2f7472617669732d63692e636f6d2f74796c65726e617468616e726565642f6c61726176656c2d72656c6174696f6e2d6a6f696e732e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/tylernathanreed/laravel-relation-joins)

This package adds the ability to join on a relationship by name.

Introduction
------------

[](#introduction)

Eloquent doesn't offer any tools for joining, so we've been stuck with the base query builder joins. While Eloquent does have the "has" concept for existence, there are still times where you want to return information about the related entities, or aggregate information together.

I've seen other packages out there that try to accompish a goal similar to this one. I tried to get on board with at least one of them, but they all fell short for a number of reasons. Let me first explain the features of this package, and you might see why this one is better (at least what for what I intend to use it for).

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

[](#installation)

#### Using Composer

[](#using-composer)

```
composer require ahfa92/laravel-relation-joins

```

DISCLAIMER: THIS PACKAGE ONLY FOR FAST PATCHING IF YOU WANT TO HAVE STABLE PACKAGE USE COMPOSER PACKAGE BELOW!!!

```
composer require reedware/laravel-relation-joins

```

#### Versioning

[](#versioning)

This package was built with the latest version of Laravel in mind, but support goes back to Laravel 5.5.

Usage
-----

[](#usage)

### 1. Performing a join via relationship

[](#1-performing-a-join-via-relationship)

This is the entire point of this package, so here's a basic example:

```
User::query()->joinRelation('posts');
```

This will apply a join from the `User` model through the `posts` relation, leveraging any query scopes (such as soft deletes) automatically.

You can perform joins over all relationship types (except MorphTo, which "has" doesn't support either), including the new "HasOneThrough" relationship. Additionally, you can perform the other types of joins, using a syntax similar to the base query builder:

```
User::query()->leftJoinRelation('posts');
User::query()->rightJoinRelation('posts');
User::query()->crossJoinRelation('posts');
```

### 2. Joining to nested relationships

[](#2-joining-to-nested-relationships)

One of the shining abilities of being able to join through relationships shows up when you have to navigate through a nested web of relationships. When trying to join on a relation through another relation, you can use the "dot" syntax, similar to how the "has" and "with" concepts work:

```
User::query()->joinRelation('posts.comments');
```

### 3. Adding join constraints

[](#3-adding-join-constraints)

This is honestly where I felt a lot of the existing solutions were lacking. They either created custom "where" clauses, or limited the query to only supporting certain types of "where" clauses. With this package, there are no known restrictions, and the means of adding the constraints is very intuitive:

```
User::query()->joinRelation('posts', function ($join) {
    $join->where('posts.created_at', '>=', '2019-01-01');
});
```

This will tack on the specific constraints to the already provided relationship constraints, making this really easy to use. Here are a few more examples:

```
// Disabling soft deletes for only the "Post" model
User::query()->joinRelation('posts', function ($join) {
    $join->withTrashed();
});
```

```
// Using a query scope on the "Post" model
User::query()->joinRelation('posts', function ($join) {
    $join->active();
});
```

### 4. Joining through relationships

[](#4-joining-through-relationships)

There are times where you want to tack on clauses for intermediate joins. This can get a bit tricky in some other packages (by trying to automatically deduce whether or not to apply a join, or by not handling this situation at all).

This package introduces something I'm calling a "through" join. Essentially, a "through" join indicates "I want to apply only the final relation in the 'dot' notation to my query".

Here's an example:

```
// Using a query scope on the "Post" model
User::query()->joinRelation('posts', function ($join) {
    $join->where('is_active', '=', 1);
})->joinThroughRelation('posts.comments', function ($join) {
    $join->where('comments.title', 'like', '%looking for something%');
});
```

The second part, `joinThroughRelation`, will only apply the `comments` relation join, but it will do so as if it came from the `Post` model.

### 5. Joining on circular relationships

[](#5-joining-on-circular-relationships)

This package also supports joining on circular relations, and handles it the same way the "has" concept does:

```
public function employees()
{
    return $this->hasMany(static::class, 'manager_id', 'id');
}

User::query()->joinRelation('employees');

// SQL: select * from "users" inner join "users" as "laravel_reserved_0" on "laravel_reserved_0"."manager_id" = "users"."id"
```

Now clearly, if you're wanting to apply constraints on the `employees` relation, having this sort of naming convention isn't desirable. This brings me to the next feature:

### 6. Aliasing joins

[](#6-aliasing-joins)

You could alias the above example like so:

```
User::query()->joinRelation('employees as employees');

// SQL: select * from "users" inner join "users" as "employees" on "employees"."manager_id" = "users"."id"
```

The join doesn't have to be circular to support aliasing. Here's an example:

```
User::query()->joinRelation('posts as articles');

// SQL: select * from "users" inner join "posts" as "articles" on "articles"."user_id" = "users"."id"
```

This also works for nested relations:

```
User::query()->joinRelation('posts as articles.comments as feedback');

// SQL: select * from "users" inner join "posts" as "articles" on "articles"."user_id" = "users"."id" inner join "comments" as "feedback" on "feedback"."post_id" = "articles"."id"
```

For relations that require multiple tables (i.e. BelongsToMany, HasManyThrough, etc.), the alias will apply to the far/non-pivot table. If you need to alias the pivot/through table, you can use a double-alias:

```
public function roles()
{
    return $this->belongsToMany(EloquentRoleModelStub::class, 'role_user', 'user_id', 'role_id');
}

User::query()->joinRelation('roles as users_roles,roles');
// SQL: select * from "users" inner join "role_user" as "users_roles" on "users_roles"."user_id" = "users"."id" inner join "roles" on "roles"."id" = "users_roles"."role_id"

User::query()->joinRelation('roles as users_roles,positions');
// SQL: select * from "users" inner join "role_user" as "position_user" on "position_user"."user_id" = "users"."id" inner join "roles" as "positions" on "positions"."id" = "position_user"."role_id"
```

### 7. Everything else

[](#7-everything-else)

Everything else you would need for joins: aggregates, grouping, ordering, selecting, etc. all go through the already established query builder, where none of that was changed. Meaning I can easily do something like this:

```
User::query()->joinRelation('licenses')->groupBy('users.id')->orderBy('users.id')->select('users.id')->selectRaw('sum(licenses.price) as revenue');
```

Personally, I see myself using this a ton in Laravel Nova (specifically lenses), but I've been needing queries like this for years in countless scenarios.

Joins are something that nearly every developer will eventually use, so having Eloquent natively support joining over relations would be fantastic. However, since that doesn't come out of the box, you'll have to install this package instead. My goal with this package is to mirror the Laravel "feel" of coding, where complex implementations (such as joining over named relations) is simple to use and easy to understand.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 93.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~48 days

Recently: every ~28 days

Total

11

Last Release

1988d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/35247436?v=4)[ahfa92](/maintainers/ahfa92)[@ahfa92](https://github.com/ahfa92)

---

Top Contributors

[![tylernathanreed](https://avatars.githubusercontent.com/u/6486381?v=4)](https://github.com/tylernathanreed "tylernathanreed (44 commits)")[![ahfa92](https://avatars.githubusercontent.com/u/35247436?v=4)](https://github.com/ahfa92 "ahfa92 (2 commits)")[![hasnhasan](https://avatars.githubusercontent.com/u/3776411?v=4)](https://github.com/hasnhasan "hasnhasan (1 commits)")

---

Tags

laraveleloquentqueryjoinrelation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ahfa92-laravel-relation-joins/health.svg)

```
[![Health](https://phpackages.com/badges/ahfa92-laravel-relation-joins/health.svg)](https://phpackages.com/packages/ahfa92-laravel-relation-joins)
```

###  Alternatives

[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[kirschbaum-development/eloquent-power-joins

The Laravel magic applied to joins.

1.6k25.2M34](/packages/kirschbaum-development-eloquent-power-joins)[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[mehdi-fathi/eloquent-filter

Eloquent Filter adds custom filters automatically to your Eloquent Models in Laravel.It's easy to use and fully dynamic, just with sending the Query Strings to it.

450191.6k1](/packages/mehdi-fathi-eloquent-filter)[cybercog/laravel-love

Make Laravel Eloquent models reactable with any type of emotions in a minutes!

1.2k302.7k1](/packages/cybercog-laravel-love)[cviebrock/eloquent-taggable

Easy ability to tag your Eloquent models in Laravel.

567694.8k3](/packages/cviebrock-eloquent-taggable)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
