/关系

从给定的 Laravel 模型中提取关系

1.0.0 2023-02-10 20:02 UTC

README

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

该包旨在使用反射和返回类型检查从给定的模型中提取 Eloquent 关系。

安装

您可以通过 composer 安装此包

composer require plank/larelations

用法

给定一个 Eloquent 模型实例或其类字符串,此包将提取模型上定义的所有关系,并以 RelationInstance 项的集合形式返回。

每个 RelationInstance 都有一个定义关系的反射方法 ($method) 以及一个 Relation (关系) 实例。在 RelationInstance 上有一些辅助方法,允许您分类关系的类型(例如,子关系、父关系、交叉关系等)。

$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
}

测试

composer test

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件

安全漏洞

如果您在 Larelations 中发现安全漏洞,请发送电子邮件至 security@plankdesign.com。所有安全漏洞都将得到及时处理。