PHPackages                             n0n0n0n0/with-join - 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. n0n0n0n0/with-join

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

n0n0n0n0/with-join
==================

Package to convert Eloquent BelongsTo subqueries into one query with left join for Laravel 5+.

2.0.0(8y ago)135MITPHP

Since Nov 12Pushed 7y ago1 watchersCompare

[ Source](https://github.com/n0n0n0n0/with-join)[ Packagist](https://packagist.org/packages/n0n0n0n0/with-join)[ RSS](/packages/n0n0n0n0-with-join/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (1)Versions (15)Used By (0)

Package to convert Eloquent BelongsTo subqueries into one query with left join for Laravel 5.5
----------------------------------------------------------------------------------------------

[](#package-to-convert-eloquent-belongsto-subqueries-into-one-query-with-left-join-for-laravel-55)

[![Build Status](https://camo.githubusercontent.com/521475f10b4a5690ce87667678c582a6bcf9c01954c3ddf15082191bb9aa652d/68747470733a2f2f7472617669732d63692e6f72672f736c656570696e672d6f776c2f776974682d6a6f696e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/sleeping-owl/with-join)[![Latest Stable Version](https://camo.githubusercontent.com/af54d72129d9ed65dc66d1a8b5b931a4266f7dd92f512fda5352f2aecb74df78/68747470733a2f2f706f7365722e707567782e6f72672f736c656570696e672d6f776c2f776974682d6a6f696e2f762f737461626c652e737667)](https://packagist.org/packages/sleeping-owl/with-join)[![License](https://camo.githubusercontent.com/52b77ca447fda96fe3b68f7bf953df521fec83c2da347891205945661f05a736/68747470733a2f2f706f7365722e707567782e6f72672f736c656570696e672d6f776c2f776974682d6a6f696e2f6c6963656e73652e737667)](https://packagist.org/packages/sleeping-owl/with-join)[![Code Climate](https://camo.githubusercontent.com/6e4b3b049e221c7def0600bf4f1f1bda75f4516f89c2cf1017be190a2a7bcb5b/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f736c656570696e672d6f776c2f776974682d6a6f696e2f6261646765732f6770612e737667)](https://codeclimate.com/github/sleeping-owl/with-join)

### Usage

[](#usage)

Mark relation you want to convert into left join using `->references($relations)` method or `Model::includes($relations)` method:

```
Model::with('other')->references('other')->orderBy('other.title', 'asc')->get();
 # this will make one sql-query with left join of 'other' relation
 # result object will be the same object

Model::includes('first', 'second')->where('first.title', '=', 'my title')->get();
 # will be the same as Model::with('first', 'second')->references('first', 'second')->…

Model extends Eloquent
{
	$includes = ['first', 'second'];
}
Model::where('first.title', '=', 'my title')->get();
# result is same as Model::includes but definition is done within the model
# if you use $with and $includes together it will be merged

Model::with('foreign')->orderBy('field', 'asc')->get();
 # this will work with default behaviour (perform 2 sql-queries)
```

### Example

[](#example)

#### New Behaviour

[](#new-behaviour)

```
StreetImage::includes('street')->first()
```

will perform the following sql-query:

```
select
	`street`.`` as `__f__street---`,
	`street_images`.*
from
	`street_images`
left join
	`streets` as `street` on `street`.`id` = `street_images`.`street_id`
order by `sort` asc
limit 1
```

#### Default Behaviour

[](#default-behaviour)

```
StreetImage::with('street')->first()
```

will perform the following sql-queries:

```
select `street_images`.* from `street_images` order by `sort` asc limit 1
select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc
```

### Object Structure

[](#object-structure)

You will get the same object if you will use `includes()` method. For example:

```
StreetImage::includes('street')->first()
```

will return:

```
object(StreetImage) {

	street: object(Street) {

	}
}

```

Structure will be the same even if you using nested relations:

```
StreetImage::includes('street.district')->first();
```

will return:

```
object(StreetImage) {

	street: object(Street) {

		district: object(District) {

		}
	}
}

```

### Nested Relations

[](#nested-relations)

```
StreetImage::includes('street.type', 'street.district')->first();
```

will perform a following sql-query (*&lt;…&gt; will be replaced with all table columns*):

```
select
	 `street`.`` as `__f__street---`,
	 `type`.`` as `__f__street---__f__type---`,
	 `district`.`` as `__f__street---__f__district---`,
	 `street_images`.*
from
	`street_images`
left join
	`streets` as `street` on `street`.`id` = `street_images`.`street_id`
left join
	`street_types` as `type` on `type`.`id` = `street`.`street_type_id`
left join
	`districts` as `district` on `district`.`id` = `street`.`district_id`
order by `sort` asc
limit 1
```

instead of performing 4 sql-queries by default Eloquent behaviour:

```
select `street_images`.* from `street_images` order by `sort` asc limit 1

select `streets`.* from `streets` where `streets`.`id` in (?) order by `title` asc

select * from `street_types` where `street_types`.`id` in (?) order by `title` asc

select * from `districts` where `districts`.`id` in (?) order by `sort` asc
```

### Installation

[](#installation)

1. Require this package in your composer.json and run composer update (or run `composer require sleeping-owl/with-join:1.x` directly):

    ```
     "sleeping-owl/with-join": "1.*"

    ```
2. Use `\Nonono\WithJoin\WithJoinTrait` trait in every eloquent model you want to use this package features:

    ```
    class StreetImage extends \Eloquent
    {
    	use \Nonono\WithJoin\WithJoinTrait;
    }
    ```
3. That`s all.

Copyright and License
---------------------

[](#copyright-and-license)

Package was written by Sleeping Owl for the Laravel framework and is released under the MIT License. See the LICENSE file for details.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% 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 ~91 days

Recently: every ~252 days

Total

13

Last Release

3103d ago

Major Versions

1.0.11 → 2.0.02017-11-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/84fca816b1ad4e5999518874178ea9acf240e83e9185db490c674645742962cf?d=identicon)[n0n0n0n0](/maintainers/n0n0n0n0)

---

Top Contributors

[![sleeping-owl](https://avatars.githubusercontent.com/u/9197310?v=4)](https://github.com/sleeping-owl "sleeping-owl (34 commits)")[![DeadeyeBro](https://avatars.githubusercontent.com/u/175374031?v=4)](https://github.com/DeadeyeBro "DeadeyeBro (9 commits)")[![delaet](https://avatars.githubusercontent.com/u/5644283?v=4)](https://github.com/delaet "delaet (5 commits)")[![Tom5om](https://avatars.githubusercontent.com/u/4668890?v=4)](https://github.com/Tom5om "Tom5om (2 commits)")[![zakhenry](https://avatars.githubusercontent.com/u/721513?v=4)](https://github.com/zakhenry "zakhenry (1 commits)")

---

Tags

laraveleloquentjoinactiverecordwith

### Embed Badge

![Health badge](/badges/n0n0n0n0-with-join/health.svg)

```
[![Health](https://phpackages.com/badges/n0n0n0n0-with-join/health.svg)](https://phpackages.com/packages/n0n0n0n0-with-join)
```

###  Alternatives

[sleeping-owl/with-join

Package to convert Eloquent BelongsTo subqueries into one query with left join.

9488.3k9](/packages/sleeping-owl-with-join)[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[bavix/laravel-clickhouse

Eloquent model for ClickHouse

72214.1k2](/packages/bavix-laravel-clickhouse)[msafadi/laravel-eloquent-join-with

Laravel Eloquent Join With Relationships

1646.0k](/packages/msafadi-laravel-eloquent-join-with)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

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