LaravelPackages.net
Acme Inc.
Toggle sidebar
plank/larelations

Extract the relations from a given Laravel model

35
6
1.0.0
About plank/larelations

plank/larelations is a Laravel package for extract the relations from a given laravel model. It currently has 6 GitHub stars and 35 downloads on Packagist (latest version 1.0.0). Install it with composer require plank/larelations. Discover more Laravel packages by plank or browse all Laravel packages to compare alternatives.

Last updated

Larelations

Laravel Version Support GitHub Workflow Status Code Climate coverage Code Climate maintainability

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 [email protected]. All security vulnerabilities will be promptly addressed.

Star History Chart