ahnify / laravel-morphable
easy querying on polymorphic relations
Requires
- php: >=7.0
- illuminate/database: ~5.5.0|~5.6.0|~5.7.0|~5.8.0
Requires (Dev)
- orchestra/testbench: ~3.4.2|~3.5.0|~3.6.0|~3.7.0|~3.8.0
- phpunit/phpunit: ^5.7|6.2|^7.0|^8.0
This package is auto-updated.
Last update: 2021-09-08 15:05:54 UTC
README
Morphable
This package provides a trait that adds query scopes to an Eloquent model for easy querying on polymorphic relations.
Installation
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.
Usage
To query on polymorphic relation you must:
- add the trait
Ahnify\Morphable\MorphableTrait
to your model.
Example
use Ahnify\Morphable\MorphableTrait class ExampleModel extends Eloquent { use MorphableTrait; ... }
that's it.
now you can query on your relation like this:
ExampleModel::whereMorphable('transactionable',BankTransaction::class,function($query){ $query->where('amount','>', 30 ); })->get()
this is equivalent to this for non polymorphic relations:
ExampleModel::whereHas('bankTransaction',function($query){ $query->where('amount','>', 30 ); })->get()
also, we can chain it too:
ExampleModel::query() ->whereMorphable('transactionable',BankTransaction::class,function($query){ $query->where('amount','>', 30 ); }) ->orWhereMorphable('transactionable',OnlineTransaction::class,function($query){ $query->where('created_date','>', '2019-01-01' ); }) ->get()
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:
ExampleModel::whereMorphable('transactionable',[BankTransaction::class,OnlineTransaction::class],function($query){ $query->where('amount','>', 30 ); })->get() // 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 :
ExampleModel::whereMorphable('transactionable',['bankTransaction','onlineTransaction'],function($query){ $query->where('amount','>', 30 ); })->get()