plank / larelations
Extract the relations from a given Laravel model
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- staudenmeir/belongs-to-through: ^2.12
This package is auto-updated.
Last update: 2024-11-29 12:24:06 UTC
README
This package is designed to extract Eloquent Relations from a given Model using reflection and return type checking.
Installation
You can install the package via composer:
composer require plank/larelations
Usage
Given an instance of an Eloquent Model or its class-string, this package will extract all relations defined on the model, and return them in a Collection of RelationInstance
items.
Each RelationInstance
has the ReflectionMethod ($method
) where the relation was defined, as well as an instance of the Relation
($relation). There are some helper methods on the RelationInstance
that allow you to classify the type relation it is (ie. child, parent, pivotted, etc).
$extractor = new \Plank\Larelations\Extractor(); $instances = $extractor->extract($post); $instances = \Plank\Larelations\Facades\Larelations::extract(Post::class); foreach ($instances as $instance) { if ($instance->isChild()) { // Handle child types of relations } if ($instance->relation instanceof \Znck\Eloquent\Traits\BelongsToThrough) { // Handle custom relation } // The method property is the \ReflectionMethod of the relation instance $instance->method->getName(); // posts }
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
Security Vulnerabilities
If you discover a security vulnerability within Larelations, please send an e-mail to security@plankdesign.com. All security vulnerabilities will be promptly addressed.