All Projects â†’ rapidwebltd â†’ Improved Polymorphic Eloquent Builder

rapidwebltd / Improved Polymorphic Eloquent Builder

Licence: lgpl-3.0
🔨 Improved Polymorphic Eloquent Builder

Projects that are alternatives of or similar to Improved Polymorphic Eloquent Builder

Laravel Database Encryption
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Stars: ✭ 238 (+1883.33%)
Mutual labels:  eloquent, laravel, laravel-5-package
Watchable
Enable users to watch various models in your application.
Stars: ✭ 65 (+441.67%)
Mutual labels:  eloquent, laravel, laravel-5-package
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+22925%)
Mutual labels:  eloquent, laravel, laravel-5-package
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+1550%)
Mutual labels:  eloquent, laravel, laravel-5-package
Lada Cache
A Redis based, fully automated and scalable database cache layer for Laravel
Stars: ✭ 424 (+3433.33%)
Mutual labels:  eloquent, laravel, laravel-5-package
Orm
A drop-in Doctrine ORM 2 implementation for Laravel 5+ and Lumen
Stars: ✭ 712 (+5833.33%)
Mutual labels:  laravel, laravel-5-package
Laravel
Laravel Model Generator
Stars: ✭ 715 (+5858.33%)
Mutual labels:  eloquent, laravel
Laravel Love
Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
Stars: ✭ 822 (+6750%)
Mutual labels:  eloquent, laravel
Identity Number
Validator for Swedish personal identity numbers (personnummer). For use "standalone" or with Laravel.
Stars: ✭ 17 (+41.67%)
Mutual labels:  laravel, laravel-5-package
Laravel Video Chat
Laravel Video Chat using Socket.IO and WebRTC
Stars: ✭ 646 (+5283.33%)
Mutual labels:  laravel, laravel-5-package
Laravel Sluggable
An opinionated package to create slugs for Eloquent models
Stars: ✭ 831 (+6825%)
Mutual labels:  eloquent, laravel
Simple Cache
An easy to use Caching trait for Laravel's Eloquent Models.
Stars: ✭ 19 (+58.33%)
Mutual labels:  eloquent, laravel
Laravel Repositories
[ABANDONED] Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Stars: ✭ 664 (+5433.33%)
Mutual labels:  eloquent, laravel
Laravel Friendships
This package gives Eloquent models the ability to manage their friendships.
Stars: ✭ 651 (+5325%)
Mutual labels:  eloquent, laravel
Laravel Widgetize
A minimal package to help you make your laravel application cleaner and faster.
Stars: ✭ 791 (+6491.67%)
Mutual labels:  laravel, laravel-5-package
Laravel Imageup
Auto Image & file upload, resize and crop for Laravel eloquent model using Intervention image
Stars: ✭ 646 (+5283.33%)
Mutual labels:  eloquent, laravel
Angular5.2 Laravel5.6
Angular 5.2 and Laravel 5.6 Authentication and CRUD
Stars: ✭ 17 (+41.67%)
Mutual labels:  laravel, laravel-5-package
Eloquent Sortable
Sortable behaviour for Eloquent models
Stars: ✭ 914 (+7516.67%)
Mutual labels:  eloquent, laravel
Validating
Automatically validating Eloquent models for Laravel
Stars: ✭ 906 (+7450%)
Mutual labels:  eloquent, laravel
Eloquent Filter
This simple package helps you filter Eloquent data using query filters.
Stars: ✭ 24 (+100%)
Mutual labels:  eloquent, laravel

Improved Polymorphic Eloquent Builder

Packagist

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

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

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

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.

variations bags sunglasses
id id id
model_type ('bags', 'sunglasses', etc.) brand_id brand_id
model_id material_id lens_colour_id
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].