PHPackages                             blasttech/eloquent-related-plus - 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. blasttech/eloquent-related-plus

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

blasttech/eloquent-related-plus
===============================

Adds search and order functionality to Laravel Eloquent related models

1.0.1(4y ago)528.8k↓100%MITPHPPHP &gt;=7.0CI failing

Since Jan 16Pushed 4y ago10 watchersCompare

[ Source](https://github.com/blasttech/eloquent-related-plus)[ Packagist](https://packagist.org/packages/blasttech/eloquent-related-plus)[ Docs](https://github.com/blasttech/eloquent-related-plus)[ RSS](/packages/blasttech-eloquent-related-plus/feed)WikiDiscussions master Synced today

READMEChangelog (6)Dependencies (5)Versions (65)Used By (0)

Add extra search and order functionality to Laravel Eloquent models.
====================================================================

[](#add-extra-search-and-order-functionality-to-laravel-eloquent-models)

[![Latest Version](https://camo.githubusercontent.com/7ff98e3113e2f39013952b676d8f6cbbb7c3f1c0831102f164434402d60418db/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f7461672f626c617374746563682f656c6f7175656e742d72656c617465642d706c75732e7376673f7374796c653d666c61742d737175617265)](https://github.com/blasttech/eloquent-related-plus/releases)[![Build Status](https://camo.githubusercontent.com/28bc456716663426449640583aae2d30052f5eaff7aa9cbf2d5d6a24c39835f1/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f626c617374746563682f656c6f7175656e742d72656c617465642d706c75732e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/blasttech/eloquent-related-plus)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![StyleCI](https://camo.githubusercontent.com/1c3e90e25544bc98a1eeba46ac4d734ac77483a3874a52b1d554b06a4b943d3d/68747470733a2f2f7374796c6563692e696f2f7265706f732f3131373735363139362f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/117756196)[![Quality Score](https://camo.githubusercontent.com/c4a6ed6cf509386746a40bf272ccc1ecfe339a7124198bd34d27f3cef2e0db4b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f626c617374746563682f656c6f7175656e742d72656c617465642d706c75732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/blasttech/eloquent-related-plus)[![Total Downloads](https://camo.githubusercontent.com/392fe5d7880f564c9ba4a7f203432161108377c20187bd146af954cbaee8297e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f626c617374746563682f656c6f7175656e742d72656c617465642d706c75732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/blasttech/eloquent-related-plus)

This package provides a trait that adds extra ways to order and search complex models. With Eloquent, when relations are used (eg. HasOne, HasMany, BelongsTo) the primary model can't be sorted using a field in a related table.

For example, you might have a list of Customers with a one-to-one relation (HasOne) to a Contacts model for the Customer contact.

```
class Customer extends Eloquent
{
    public function sales_rep()
    {
        return $this->hasOne(Contact::class, 'id', 'sales_rep_id');
    }
}
```

If you had a table of customers and wanted to sort it by Contact name, you nomrally wouldn't be able to in Laravel using the relation above, but you could with this package.

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

[](#installation)

This package can be installed through Composer.

```
$ composer require blasttech/eloquent-related-plus

```

Usage
-----

[](#usage)

To add complex order and search behaviour to your model you must:

1. specify that the model will conform to `Blasttech\EloquentRelatedPlus\RelatedPlusInterface`
2. use the trait `Blasttech\EloquentRelatedPlus\RelatedPlusTrait`

Using the earlier example:

```
use App\Contact;
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class Customer extends Eloquent implements RelatedPlusInterface
{
    use RelatedPlusTrait;

    public function sales_rep()
    {
        return $this->hasOne(Contact::class, 'id', 'sales_rep_id');
    }
}
```

modelJoin
---------

[](#modeljoin)

Use this scope to add a join for a related model.

- modelJoin($relation\_name, $operator = '=', $type = 'left', $where = false, $related\_select = true)

#### Example

[](#example)

```
namespace App\Http\Controllers;

use App\Customer;

class CustomerController extends Controller
{
    public function getCustomers()
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->get();
    }

    ...
}
```

This will add a left join to the *Contacts* model using the Contacts() relation, using 'where' instead of 'on' and include all the fields from the model in the select.

##### $relation\_name

[](#relation_name)

The name of the relation. Only BelongsTo, HasOne, HasMany or HasOneOrMany relations will work.

##### $operator

[](#operator)

The operator used to join the related table. This will default to '=' but any of the standard Laravel join operators can be used.

##### $type

[](#type)

The type of join. This will default to 'left' but any of the standard Laravel join types are allowed.

##### $where

[](#where)

The method of joining the table. The default (false) uses an 'on' statement but if true, a where statement is used.

##### $related\_select

[](#related_select)

The $related\_select option determines if the fields in the joined table will be included in the query. If true, the field names will be in the format '*table\_name.column\_name*', for example, '*customer.contact\_name*' with the table name and period (.) **included** in the field name. This is to allow fields from joined tables to be used when they have the same column names.

orderByCustom
-------------

[](#orderbycustom)

- orderByCustom($orderField, $dir, $orderFields = null, $orderDefaults = null)

#### Example

[](#example-1)

```
use App\Customer;
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class CustomerController extends Controller
{
    public function getCustomers()
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->orderByCustom('contact_name');
    }

    ...
}
```

This will add a left join to the *Contacts* model using the Contacts() relation and then sort it by the contact\_name.

search
------

[](#search)

#### Example

[](#example-2)

```
use Blasttech\EloquentRelatedPlus\RelatedPlusInterface;
use Blasttech\EloquentRelatedPlus\RelatedPlusTrait;

class MyModel extends Eloquent implements RelatedPlusInterface
{
    use RelatedPlusTrait;

    public function getContact($contact_name)
    {
        return Customer::select('*')
            ->modelJoin('Contacts', '=', 'left', false, false)
            ->search($contact_name);
    }

    ...
}
```

This will add a left join to the *Contacts* model using the Contacts() relation, and search for $contact\_name.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

Credits
-------

[](#credits)

The example shown in  was used as a basis for the modelJoin and relationJoin scopes.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity29

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 91.9% 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 ~21 days

Recently: every ~65 days

Total

56

Last Release

1805d ago

Major Versions

0.4.11 → 1.0.02021-03-31

### Community

Maintainers

![](https://www.gravatar.com/avatar/f99e8c76b2ec65ebcc2c9c346839736e22e9f5d503e23272319937bc60ffe211?d=identicon)[mhkb](/maintainers/mhkb)

---

Top Contributors

[![mhkb](https://avatars.githubusercontent.com/u/4683976?v=4)](https://github.com/mhkb "mhkb (204 commits)")[![maikenunes](https://avatars.githubusercontent.com/u/4635672?v=4)](https://github.com/maikenunes "maikenunes (7 commits)")[![ivanhennig](https://avatars.githubusercontent.com/u/6207878?v=4)](https://github.com/ivanhennig "ivanhennig (3 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (3 commits)")[![mikeybeck](https://avatars.githubusercontent.com/u/468643?v=4)](https://github.com/mikeybeck "mikeybeck (2 commits)")[![w-lopes](https://avatars.githubusercontent.com/u/7225515?v=4)](https://github.com/w-lopes "w-lopes (2 commits)")[![criszacca](https://avatars.githubusercontent.com/u/38415677?v=4)](https://github.com/criszacca "criszacca (1 commits)")

---

Tags

laravelmodeleloquentjoinorderby

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/blasttech-eloquent-related-plus/health.svg)

```
[![Health](https://phpackages.com/badges/blasttech-eloquent-related-plus/health.svg)](https://phpackages.com/packages/blasttech-eloquent-related-plus)
```

###  Alternatives

[msafadi/laravel-eloquent-join-with

Laravel Eloquent Join With Relationships

1646.0k](/packages/msafadi-laravel-eloquent-join-with)[phaza/single-table-inheritance

Single Table Inheritance Trait

1515.8k](/packages/phaza-single-table-inheritance)

PHPackages © 2026

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