PHPackages                             rapidwebltd/improved-polymorphic-eloquent-builder - 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. rapidwebltd/improved-polymorphic-eloquent-builder

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

rapidwebltd/improved-polymorphic-eloquent-builder
=================================================

Attempting to use whereHas queries with standard Eloquent polymorphic relationships will fail, due to Eloquent being unable to determine the correct model to retrieve. The 'Improved Polymorphic Eloquent Builder' is a class which extends the Eloquent Builder class that is built in to Laravel 5.1. It enables limited use of the whereHas method to query Eloquent polymorphic relationships.

v2.0.2(8y ago)111.1k1LGPL-3.0-onlyPHPPHP &gt;=5.5.9

Since Jan 25Pushed 7y agoCompare

[ Source](https://github.com/rapidwebltd/Improved-Polymorphic-Eloquent-Builder)[ Packagist](https://packagist.org/packages/rapidwebltd/improved-polymorphic-eloquent-builder)[ Docs](https://github.com/rapidwebltd/Improved-Polymorphic-Eloquent-Builder)[ RSS](/packages/rapidwebltd-improved-polymorphic-eloquent-builder/feed)WikiDiscussions master Synced yesterday

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

Improved Polymorphic Eloquent Builder
=====================================

[](#improved-polymorphic-eloquent-builder)

[![Packagist](https://camo.githubusercontent.com/0e7b0ac44d0049ce872ed5e7fe908c78c8048a3f483efa7bff7a0e8243a9e94b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72617069647765626c74642f496d70726f7665642d506f6c796d6f72706869632d456c6f7175656e742d4275696c6465722e737667)](https://camo.githubusercontent.com/0e7b0ac44d0049ce872ed5e7fe908c78c8048a3f483efa7bff7a0e8243a9e94b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72617069647765626c74642f496d70726f7665642d506f6c796d6f72706869632d456c6f7175656e742d4275696c6465722e737667)

Attempting to use `whereHas` queries with standard Eloquent polymorphic relationships will fail, due to Eloquent being unable to determine the correct model to retrieve. You may receive an error similar to the one below as Eloquent tries to build the query using columns from the model without including its table name.

```
QueryException in Connection.php line 662:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'column_name' in 'where clause'

```

The 'Improved Polymorphic Eloquent Builder' is a class which extends the Eloquent Builder class that is built in to Laravel 5.1. It enables limited use of the `whereHas` method to query Eloquent polymorphic relationships.

Requirements
------------

[](#requirements)

You must be using Laravel 5.1 as your framework and Eloquent as your ORM.

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

[](#installation)

Simply require this package, using Composer, in the root directory of your project.

```
composer require rapidwebltd/improved-polymorphic-eloquent-builder

```

Then change any Eloquent models using polymorphic relationships to extend the `\RapidWeb\ImprovedPolymorphicEloquentBuilder\Model`class. This will usually be any model(s) containing methods which return `morphTo()` relationship(s). An example class is shown below.

```
class Variation extends \RapidWeb\ImprovedPolymorphicEloquentBuilder\Model
{
  public function model()
  {
    return $this->morphTo();
  }
}
```

Usage
-----

[](#usage)

When performing a `whereHas` query, you must specify the morph types as the 5th argument. Morph types refers to an array of the polymorphic types you wish to filter by. These should be presented as strings equal to one or more of the possible values the `[...]_type` field of the polymorphic relationship in question.

The example below selects all `variation` records that have related `model` record of type `bags`, with a `brand_id` of 2.

```
Variation::whereHas('model', function($query) use ($brandId) {
  $query->where('brand_id', 2);
}, '>=', 1, ['bags'])->get();
```

A database structure for this example would be similar to the following. The polymorphic fields are highlighted in **bold**.

variationsbagssunglassesid**id****id****model\_type** ('bags', 'sunglasses', etc.)brand\_idbrand\_id**model\_id**material\_idlens\_colour\_id

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% 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 ~92 days

Total

5

Last Release

3073d ago

Major Versions

v1.0.1 → v2.0.02017-03-07

### Community

Maintainers

![](https://www.gravatar.com/avatar/c580cdf7c14898fff179cdfc1085892091d5d2f49d917873a12365af9ac77c93?d=identicon)[Jord-JD](/maintainers/Jord-JD)

---

Top Contributors

[![Jord-JD](https://avatars.githubusercontent.com/u/650645?v=4)](https://github.com/Jord-JD "Jord-JD (5 commits)")[![kirsty-gasston](https://avatars.githubusercontent.com/u/12949343?v=4)](https://github.com/kirsty-gasston "kirsty-gasston (1 commits)")

---

Tags

eloquentlaravellaravel-5-packagepolymorphic-relationshipsrelationshipslaraveleloquentlaravel-eloquentpolymorphicpolymorphism

### Embed Badge

![Health badge](/badges/rapidwebltd-improved-polymorphic-eloquent-builder/health.svg)

```
[![Health](https://phpackages.com/badges/rapidwebltd-improved-polymorphic-eloquent-builder/health.svg)](https://phpackages.com/packages/rapidwebltd-improved-polymorphic-eloquent-builder)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11223.5M33](/packages/anourvalar-eloquent-serialize)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135224.7k7](/packages/statamic-rad-pack-runway)[waad/laravel-model-metadata

A robust Laravel package for handling metadata with JSON casting, custom relation names, and advanced querying capabilities.

854.1k](/packages/waad-laravel-model-metadata)[mozex/laravel-scout-bulk-actions

Import, flush, and queue-import all your Laravel Scout searchable models at once. Auto-discovers models, runs in bulk, tracks progress.

1539.3k](/packages/mozex-laravel-scout-bulk-actions)[binafy/laravel-reactions

React to something in Laravel framework

875.6k](/packages/binafy-laravel-reactions)[stayallive/laravel-eloquent-observable

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

2933.4k9](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

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