Skip to content
Easy querying on polymorphic relations.
Branch: master
Clone or download
Ahnify Merge pull request #2 from mateusjunges/add-style-ci
Apply fixes from StyleCI, add package LICENSE and package badges.
Latest commit 9551563 Jun 8, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Apply fixes from StyleCI Jun 8, 2019
tests Apply fixes from StyleCI Jun 8, 2019
.gitignore tests(init): add tests Jun 6, 2019
.styleci.yml Add styleci laravel preset Jun 8, 2019
LICENSE Created LICENSE Jun 8, 2019 Added Bages to README file Jun 8, 2019
composer.json docs: update readme Jun 6, 2019
phpunit.xml tests(init): add tests Jun 6, 2019

Total Downloads Latest Stable Version License


This package provides a trait that adds query scopes to an Eloquent model for easy querying on polymorphic relations.


You can install this package via composer using this command:

composer require "ahnify/laravel-morphable:^0.1"

The package doesn't need to be registered.


To query on polymorphic relation you must:

  1. add the trait Ahnify\Morphable\MorphableTrait to your model.


use Ahnify\Morphable\MorphableTrait

class ExampleModel extends Eloquent

    use MorphableTrait;


that's it.

now you can query on your relation like this:

    $query->where('amount','>', 30 );

this is equivalent to this for non polymorphic relations:

    $query->where('amount','>', 30 );

also, we can chain it too:

        $query->where('amount','>', 30 );
            $query->where('created_date','>', '2019-01-01' );

also, if some of your polymorphic related to our model have common attributes, then we can query on them and pass an array of morphed class type like this:

    $query->where('amount','>', 30 );
// get all rows that have bank or online transactions that has amount more than 30  

in this example the result includes all examples that has bank or online transaction with has amount more than 30

this works also with custom types too :

    $query->where('amount','>', 30 );
You can’t perform that action at this time.